2016-01-23 149 views
1

sir我想锁定我的菜单与div ID menu固定在窗口顶部向下滚动窗口时,我也想设置位置为绝对向上滚动时I尝试使用此代码。它正确地完成了第一份工作。我可以将菜单固定在页面顶部。但它不能设置在此处向上滚动页面的绝对位置是我的代码向下滚动固定div&向上滚动时设置绝对

<script type="application/javascript"> 
    $(window).bind('scroll', function() { 
     if (window.scrollY=100){ 
      document.getElementById("menu").style.position = "fixed"; 
      document.getElementById("menu").style.top = "0px"; 
      } 
     else if(window.scrollY < 100){ 
      document.getElementById("menu").style.position = "absolute"; 
      document.getElementById("menu").style.top = "100px"; 
      } 
    }); 

    </script> 

回答

0

你是不是分配比较window.scrollY=100 价值的代码应该是:

$(window).bind('scroll', function() { 
    if (window.scrollY>=100){ 
     //   ^^-----------use >= here 
     document.getElementById("menu").style.position = "fixed"; 
     document.getElementById("menu").style.top = "0px"; 
     } 
    else if(window.scrollY < 100){ 
     document.getElementById("menu").style.position = "absolute"; 
     document.getElementById("menu").style.top = "100px"; 
     } 
});