你如何使用jQuery检测你何时接近屏幕底部? [重复]

时间:2021-07-24 06:33:29

This question already has an answer here:

这个问题在这里已有答案:

I was reading the Harvard Business Review (HBR) blog post , The Traits of Advanced Leaders (2011-02-22). They do this on The New York Times (NYT) too. How do you detect when your reader has scrolled all the way to the bottom?

我正在阅读哈佛商业评论(HBR)博客文章“高级领导者的特质”(2011-02-22)。他们也在纽约时报(纽约时报)这样做。如何检测读者何时一直滚动到底部?

On HBR, when you scroll the near the bottom, they offer you another article to read.

在HBR上,当您滚动靠近底部时,它们会为您提供另一篇文章供您阅读。

3 个解决方案

#1


15  

$(window).scroll(function(){
    if ($(window).scrollTop() == $(document).height()-$(window).height()){
        alert("We're at the bottom of the page!!");
    }
});

#2


17  

While the other answer will show you when you are at the bottom, to answer your question about how to tell when you're NEAR the bottom, I've used this before:

虽然另一个答案会告诉你什么时候你在底部,回答你关于如何判断你何时接近底部的问题,我之前使用过这个:

if  ( ($(document).height() - $(window).height()) - $(window).scrollTop() < 1000 ){
    //do stuff
}

You can change the value "1000" to whatever you want, to trigger your script when you are that many pixels away from the bottom.

您可以将值“1000”更改为您想要的任何值,以便在距离底部很多像素时触发您的脚本。

#3


4  

$(window).scroll(function () {
   if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
      alert('end of page');
   }
});

-10 indicates how far away from end of page user must be before function executes. This gives you the flexibility to adjust the behavior as needed.

-10表示在执行函数之前用户必须离页面有多远。这使您可以根据需要灵活地调整行为。

Check working example at http://jsfiddle.net/wQfMx/

#1


15  

$(window).scroll(function(){
    if ($(window).scrollTop() == $(document).height()-$(window).height()){
        alert("We're at the bottom of the page!!");
    }
});

#2


17  

While the other answer will show you when you are at the bottom, to answer your question about how to tell when you're NEAR the bottom, I've used this before:

虽然另一个答案会告诉你什么时候你在底部,回答你关于如何判断你何时接近底部的问题,我之前使用过这个:

if  ( ($(document).height() - $(window).height()) - $(window).scrollTop() < 1000 ){
    //do stuff
}

You can change the value "1000" to whatever you want, to trigger your script when you are that many pixels away from the bottom.

您可以将值“1000”更改为您想要的任何值,以便在距离底部很多像素时触发您的脚本。

#3


4  

$(window).scroll(function () {
   if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
      alert('end of page');
   }
});

-10 indicates how far away from end of page user must be before function executes. This gives you the flexibility to adjust the behavior as needed.

-10表示在执行函数之前用户必须离页面有多远。这使您可以根据需要灵活地调整行为。

Check working example at http://jsfiddle.net/wQfMx/