我已经检查了其他帖子以及 JS 文档,这应该可以工作,但是...... console.log 在我滚动之前立即触发。
有什么想法吗?
JSFiddle:https://jsfiddle.net/zfym8g2f/
var element = document.getElementById('plugin');
if (element.scrollHeight - element.scrollTop === element.clientHeight)
{
// element is at the end of its scroll, load more content
console.log('scrolled');
}
#plugin {
height: 2000px;
}
<div id="plugin">test</div>
最佳答案
console.log 会立即触发,因为 if 语句的条件为 true。
您还需要添加滚动事件监听器,如下所示:
JSFiddle:https://jsfiddle.net/0383hfrL/
var element = document.getElementById('plugin');
window.addEventListener('scroll', function() {
if ((window.scrollY + window.innerHeight - element.offsetTop) > element.clientHeight - 150) {
// element is at the end of its scroll, load more content
console.log('scrolled');
}
希望对您有所帮助。
关于javascript - 为什么 javascript 检测到我已经滚动到元素底部,而实际上我还没有滚动到元素底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45698878/