当前位置:Gxlcms > JavaScript > 当滚动条滚动到页面底部自动加载增加内容的js代码_javascript技巧

当滚动条滚动到页面底部自动加载增加内容的js代码_javascript技巧

时间:2021-07-01 10:21:17 帮助过:35人阅读

1,注册页面滚动事件,window.onscroll = function(){ };

2,相关获取页面高度、滚动条位置、文档高度的函数:
代码如下:

//获取滚动条当前的位置
function getScrollTop() {
var scrollTop = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
}
else if (document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
}

//获取当前可是范围的高度
function getClientHeight() {
var clientHeight = 0;
if (document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
}
else {
clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
return clientHeight;
}

//获取文档完整的高度
function getScrollHeight() {
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}

3,在html页面底部增加代码:
代码如下: