14

我用它来检测滚动到页面底部。但是如何检测到页面底部的距离?

if($(window).scrollTop() + $(window).height() == $(document).height() ) {
 .. my ajax here
}

我的意思是我希望函数在他离底部 100px 或 200px 时执行,而不是在页面的最底部。我将如何改变这个?

另外:如果滚动是在显示加载内容的特定元素(比如我的大容器)的最后一个,是否可以捕获。

提前致谢

4

2 回答 2

20
var nearToBottom = 100;

if ($(window).scrollTop() + $(window).height() > 
    $(document).height() - nearToBottom) { 
 .. my ajax here 
} 

或滚动到 .CONTAINER 的底部

if ($(window).scrollTop() + $(window).height() >= 
    $('.CONTAINER').offset().top + $('.CONTAINER').height() ) { 
 .. my ajax here 
} 
于 2012-10-24T21:26:22.270 回答
5

这将对您有所帮助。

<script>
 $(window).scroll(function () {
    if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
        alert("End Of The Page");
    }
 });
</script>

通过此链接查看整篇文章并详细说明其工作原理。

当浏览器滚动到jQuery中的页面末尾时加载更多内容

于 2013-05-28T13:10:01.393 回答