2014-01-19 51 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'),也可以:如果您反复使用它们,请缓存变量。 –