【问题标题】:Checking is near/at the bottom of the page检查在页面底部附近/底部
【发布时间】:2019-07-30 07:13:16
【问题描述】:

我需要检查一个元素是否距离页面底部 x 像素,以动态加载新内容。目前,即使 bar 位于底部,scrollTop 和 height 也不匹配。

允许使用 jquery,但基本的 javascript 会更有帮助。

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

您可能想尝试以下方法(仅在 Firefox 3.5 和 IE 8 中测试):

function pollScrollPosition() {
   var y = (document.all) ? document.body.scrollTop : window.pageYOffset;
   var max = window.scrollMaxY || 
             (document.body.scrollHeight - document.body.clientHeight);

   if ((max - y) < 100) {
      console.log('Within the bottom 100 pixels. Do Something!');
   }
}

// Check the scroll position every 250ms
setInterval(pollScrollPosition, 250);

Firebug 中上述示例的屏幕截图:

【讨论】:

  • 对不起,我使用的是 webkit 浏览器。 "window.scrollMaxY" 未定义,并且 (document.body.scrollHeight - document.body.clientHeight) = 0。这很令人困惑,因为与其他元素相比,body 的 clientHeight 是不同的。
  • @DanielVassallo 这似乎不再起作用。它总是告诉我我在页面的底部,即使我在一个非常长的页面的顶部。记录这些值会使它们看起来都在获取页面高度,而不是用户在页面上的位置。
  • 我已经简化了代码并创建了这个var pixelsFromBottom = document.body.scrollHeight - (y + window.innerHeight);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
相关资源
最近更新 更多