确定何时使用Javascript滚动到页面底部
我试图确定何时滚动到页面底部(没有使用任何JS库),但到目前为止,我对使用以下哪一个有点困惑。我见过的最有希望的是确定何时使用Javascript滚动到页面底部,javascript,pagination,Javascript,Pagination,我试图确定何时滚动到页面底部(没有使用任何JS库),但到目前为止,我对使用以下哪一个有点困惑。我见过的最有希望的是window.scrollY,但即使滚动到页面底部,它也永远不会与window.innerHeight的值匹配。最好的方法是什么 window.innerWidth window.innerHeight window.outerWidth window.outerHeight window.scrollX window.scrollY document.body.scrollW
window.scrollY
,但即使滚动到页面底部,它也永远不会与window.innerHeight
的值匹配。最好的方法是什么
window.innerWidth
window.innerHeight
window.outerWidth
window.outerHeight
window.scrollX
window.scrollY
document.body.scrollWidth
document.body.scrollHeight
document.body.scrollTop
document.body.scrollLeft
document.body.offsetTop
document.body.offsetLeft
document.body.offsetWidth
document.body.offsetHeight
作为一个内心懒惰的人,我会在DIV的最底层放置一个元素,并在其上应用“”jQuery插件。(免责声明:我自己没有这方面的经验,但它看起来不错。) 该示例与博客条目略有不同:
$('#bottomDIV').bind('inview', function (event, visible) {
if (visible == true) {
// element is now visible in the viewport
highlightButtons(); // or whatever you want to do in the context
}
});
当
window.innerHeight
+document.body.scrollTop
大于或等于document.body.offsetHeight
时,您处于底部
但是由于IE对这些属性有问题,你需要使用其他属性,比如
document.documentElement.scrollTop
用于滚动,而document.documentElement.clientHeight
用于窗口高度
完整代码:这很好用:
window.onscroll = function()
{
var scrollHeight, totalHeight;
scrollHeight = document.body.scrollHeight;
totalHeight = window.scrollY + window.innerHeight;
if(totalHeight >= scrollHeight)
{
console.log("at the bottom");
}
}
为什么不使用jquery这样的JS库而手动处理事件呢?因为如果您不知道如何处理,您将无法构建jquery这样的库