【问题标题】:How do I use JQuery to detect if a user scrolls to the bottom of the page?如何使用 JQuery 检测用户是否滚动到页面底部?
【发布时间】:2012-03-30 15:55:23
【问题描述】:

一旦触底,有回调函数吗?

【问题讨论】:

标签: javascript jquery browser scroll


【解决方案1】:

您可以在窗口上以这种方式使用 .scroll() 事件:

$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       alert("bottom!");
   }
});

查看live demo

要检测用户是否在页面下方 3/4,您可以试试这个

$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() > $(document).height() - .75*$(document).height()) {
       alert("3/4th of bottom!");
   }
});

【讨论】:

  • 通常它可以工作,但“警报”它使我的 FF 崩溃,它实际上是“停电”。但我用 console.log 测试过,效果很好。
  • 谢谢。这行得通!如果我想检测用户是否在页面下方 3/4 处怎么办?
  • 我认为您在页面下方 3/4 处的解决方案实际上是在寻找“距页面底部 75 像素”。将 $(document).height() - 75 更改为 $(document).height() * 0.75 应该可以解决问题。
  • 现场演示对我不起作用...如果有帮助,Chrome 44.0.2403.125)
  • 这对我来说在任何地方都非常有用,但在我的手机上。如果我能找出问题所在,我会发布更新。
猜你喜欢
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 2021-08-21
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多