jQuery 当用户滚动页面“靠近”底部时加载更多内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13057910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:15:27  来源:igfitidea点击:

Load more content when user scrolls "near" bottom of page?

jquery

提问by Ahmed Fouad

I use this to detect scroll to the bottom of page. But how do I detect a distance from the bottom of the page?

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

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

I mean i want the function to execute when he is 100px or 200px from the bottom, and not in the very bottom of page. How would I change this?

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

Also: is it possible, to instead catch if the scroll is at the last of a specific element (say my big CONTAINER) which shows the loaded content.

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

Thanks in advance

提前致谢

回答by Doug

var nearToBottom = 100;

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

or to scroll to the bottom of .CONTAINER

或滚动到 .CONTAINER 的底部

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

回答by Rohit

This will help you.

这会帮助你。

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

Go through this link for whole article and details how it works.

通过此链接查看整篇文章并详细说明它是如何工作的。

load more content when browser scroll to end of page in jquery

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