社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

使用css和jquery平滑过渡

Scott A • 4 年前 • 240 次点击  

我正在使用jquery更改 header 取决于滚动位置的固定标题上的元素。我已经得到了我想要的代码来进行更改,但是更改是突然的,而不是平滑的。我意识到我需要使用 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>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38248
 
240 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Matej J
Reply   •   1 楼
Matej J    5 年前

你需要包括 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;
}