当用户滚动"靠近"页面底部时,加载更多内容?

Ahm*_*uad 12 jquery

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

if($(window).scrollTop() + $(window).height() == $(document).height() ) {
 .. my ajax here
}
Run Code Online (Sandbox Code Playgroud)

我的意思是我希望函数在从底部开始是100px或200px时执行,而不是在页面底部.我该如何改变?

另外:是否有可能,如果滚动位于特定元素(比如我的大容器)的最后一个,则显示加载的内容.

提前致谢

Dou*_*oug 17

var nearToBottom = 100;

if ($(window).scrollTop() + $(window).height() > 
    $(document).height() - nearToBottom) { 
 .. my ajax here 
} 
Run Code Online (Sandbox Code Playgroud)

或滚动到.CONTAINER的底部

if ($(window).scrollTop() + $(window).height() >= 
    $('.CONTAINER').offset().top + $('.CONTAINER').height() ) { 
 .. my ajax here 
} 
Run Code Online (Sandbox Code Playgroud)


小智 5

这会对你有所帮助.

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

浏览此链接以查看整篇文章,并详细说明其工作原理.

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