【问题标题】:Smooth transition using CSS and Jquery使用 CSS 和 Jquery 平滑过渡
【发布时间】:2018-10-22 16:13:00
【问题描述】:

我正在使用 jQuery 根据滚动位置更改固定标题上的 header 元素的大小。我已经让我的代码按照我的意愿进行更改,但是更改是突然的而不是平滑的。我意识到我需要在 CSS 中使用 transition 属性,但无法弄清楚在哪里或如何做。一点帮助将不胜感激。

$(window).scroll(function() {
  if ($(window).scrollTop() > 200) {
    $('.bg').addClass('show')
    $('#headerTitle').css("font-size", "1.5em");
  } else {
    $('.bg').removeClass('show')
    $('#headerTitle').css('font-size', "2em")
  };
});

$('.scroll').on('click', function(e) {
  e.preventDefault()

  $('html, body').animate({
    scrollTop: $(this.hash).offset().top
  }, 1000);
});
.transition {
  -webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;
}

.show {
  background-color: #000;
  width: 100%;
  height: 8.5em;
  opacity: 0.8;
}

#headerTitle {
  font-size: 2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="bg transition">
  <p style="width: 60%; margin-left: 20%; border-bottom: 1px solid #FFF; padding-bottom: 1em;">
    <span id="headerTitle">Joe Blow</span>
  </p>
</header>

【问题讨论】:

    标签: jquery css transition


    【解决方案1】:

    您需要在标题样式中包含transition 属性:

    #headerTitle {
    font-size: 2em;
      -webkit-transition: all 1s ease-in-out;
      -moz-transition: all 1s ease-in-out;
      -o-transition: all 1s ease-in-out;
      transition: all 1s ease-in-out;
    }
    

    【讨论】:

    • 效果很好,谢谢。我知道我需要在某处添加过渡属性,但不确定在哪里。
    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2016-02-17
    • 2021-09-14
    • 2016-05-03
    • 2013-07-19
    • 1970-01-01
    相关资源
    最近更新 更多