我需要检查元素是否距离页面底部 x 像素,以动态加载新内容。目前,即使栏位于底部,scrollTop 和高度也不匹配。
jquery 是允许的,虽然基本的 javascript 会更有帮助。
最佳答案
您可能想尝试以下方法(仅在 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 中上述示例的屏幕截图:
关于javascript - 检查在页面底部附近/底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3057892/