2014-01-19 56 views
2

我不能得到這個工作,是什麼問題?jquery查找div是否達到滾動底部

$("#scrollingbox").scroll(function() { //detect page scroll 

    if($("#scrollingbox").scrollTop() + $("#scrollingbox").height() == $("#scrollingbox").height()) //user scrolled to bottom of the page? 
    { 
      //do something 
     } 
} 

發生了什麼是我需要再次向上滾動它才能檢測到它到達底部。

+0

scrollTop的總是> = 0。所以基本上你問如果東西總是> = 0 + X等於X,當滾動是== 0,這一條件將是真實的。 IE當沒有滾動完成。你可以看到這與檢測達到底部不同。 – Petruza

回答

5

http://jsfiddle.net/collabcoders/v2RbN/1/

$("span").hide(); 

$(".box").scroll(function() { 
    if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) { 
     $("span").show();  
    } else { 
     $("span").hide(); 
    } 
}) 
+0

如果你想使用DOM方法,不要創建一個jQuery對象,然後扔掉它,只需使用DOM:'this.scrollHeight'(而不是'$(this)[0] .scrollHeight'),也可以:如果您反覆使用它們,請緩存變量。 –