2016-01-23 156 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"; 
     } 
});