【问题标题】:why does my scrollTop for detecting when I scroll to the bottom of the page not work?为什么当我滚动到页面底部时,用于检测的 scrollTop 不起作用?
【发布时间】:2013-10-09 09:33:58
【问题描述】:

我目前正在使用一些 js 来检测我何时到达我正在开发的网站的页面底部。我目前正在这里用这个脚本测试 js:

$(window).scroll(function(){
  if  ($(window).scrollTop() == $(document).height() - $(window).height() ){
    console.log('you are at the bottom');
  }
});

我的问题是它只在我到达滚动页面的顶部而不是底部时触发,这不是我想要的效果。

谁能看出它为什么不工作?

谢谢, 标记

【问题讨论】:

标签: javascript jquery scroll scrolltop


【解决方案1】:

因为scrollTop是你窗口的顶部,当你到达页面底部时,这并不与底部对齐。

您需要考虑窗口高度。

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

【讨论】:

  • 这很奇怪,因为只有当我滚动到顶部时才有效。
  • 你有没有在 $(window).load(function(){ }); ?
  • 嗯,这个 jQuery 是正确的,我已经在这里运行它,所以我建议将它包含在 window.load 中,将代码移动到页面底部等。我无法帮助你更多内容,而不会看到您的标记。
猜你喜欢
  • 2012-10-04
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多