这个特效怎么做

阅读 3.9k
5 个回答

监听scroll事件,获取到scrollTop,将其设置成header的top值,思路大概是这样。

$(window).scroll(function(){
         var scrollTop =$(this).scrollTop(); //获得滚动条距离顶部的位置
         if(scrollTop == 0) {
                 //回到顶部时的操作
         }
         else {
             //往下拉了,就在这里写改变头部的样式
         }
     });

这个网页的源码居然压缩了。。。

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js&quot;></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style>
.container{
width:100%;
height:60px;
background: #bf0;
position: fixed;
top:0;
}
.gg{
width:100%;
height:100px;
background: #000;
position: fixed;
top:60px;

}

.cc{
height:1280px;
}
</style>
</head>
<body>
<div class="container">包打听</div>
<div class="cc"></div>
<div class="gg">不要打听我</div>
<script>
$(window).scroll(function(){

   
     if($(this).scrollTop() == 0) {
             $(".gg").css("height","60px"); //返回顶部时变正常
     }
     else {
         $(".gg").css("height","30px");// 给高度设置小的
     }
 });

</script>
</body>
</html>

    $(window).scroll(function(){
          height = $(window).scrollTop();
               if(height > 45){//滚动条滚动距离
                   ...//滚动条移动大于45px你需要的样式变化
               }else{
                   ...//小于等于45px需要样式变化
               };

});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题