2013-11-27 145 views
0

我能夠加載我的ajax當滾動到底部時,我想弄清楚如何修改下面的代碼片段,以便它只在窗口滾動時才起作用到頂部?JQuery窗口滾動頂部和底部

$(window).scroll(function() { 
    if ($(document).height() <= $(window).scrollTop() + $(window).height()) { 
     //this works here for scrolling bottom 
    } 
    else if ($(document).height() >= $(window).scrollTop() + $(window).height()){ 

     //i tried checking for greater than the window scroll but that didn't owrk 
    } 
}); 

回答

3

scrollTop()返回滾動條的垂直位置就意味着滾動條處於頂部位置。

$(window).scroll(function() { 
    if ($(window).scrollTop() == 0){ 
     alert("Up"); 
    } 
}); 

或者,您可以按以下步驟更新您的代碼,

$(window).scroll(function() { 
    if ($(window).scrollTop() + $(window).height() < $(document).height()){ 
     alert("Up"); 
    //i tried checking for greater than the window scroll but that didn't work 
    } 
}); 
0

入住這或許你應該檢查一下文件和窗口對象的高度,以確保他們不爲空。

$(window).scroll(function() { 
      if ($(document).height() <= Number($(window).scrollTop() + $(window).height())) { 
       //this works here for scrolling bottom 
      } 
// only greater i think, not equa 
      else if ($(document).height() > Number($(window).scrollTop() + $(window).height())){ 

      } 
     }); 
+0

這不會解決問題,爲的$(document).height()總是會在這種情況下,最大或同等價值與增加的其他數值相比。所以問題在於if-else-if的條件。 –

相關問題