首页 > 关于scroll()事件有点疑问

关于scroll()事件有点疑问

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style type="text/css">
    *{padding:0; margin:0; list-style:none;}
    .header{ background:black; height:60px; position:relative; width:100%; left:0; top;0;}
</style>
<body>
    <div class="header">
        
    </div>
    <div class="center" style="height:1000px;"></div>
    <div class="footer"></div>
    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
    $(function(){
      $(window).scroll(function(){
          var Top=$(window).scrollTop();
          var Height=$(".header").height();
          document.title=Top+"+"+parseInt(Height);
          if(Top>Height){
             $(".header").css({"position":"fixed"});    
             $(".header").animate({opacity:1},1000);
          }else{
             $(".header").css({"position":"relative"});
             $(".header").animate({opacity:0.5},1000);
          }
      })
    })
    </script>
</body>
</html>

当Top>Height时或Top<Height时关于透明度的动画执行不流畅,甚至有时直接不执行,哪位道友知道原因?


执行不流畅是因为改变了position导致浏览器在scorll回调下频繁重绘视图。

试试加个setTimeout作为缓冲, 然后把postion的换种方法实现。


可以考虑先执行一个动画的stop操作

【热门文章】
【热门文章】