// 记录前一个滚动位置
var beforeScrollTop = 0;
function handleScroll () {
// 距顶部
var scrollTop =
document.documentElement.scrollTop || document.body.scrollTop;
// 可视区高度
var clientHeight =
document.documentElement.clientHeight || document.body.clientHeight;
// 滚动条总高度
var scrollHeight =
document.documentElement.scrollHeight || document.body.scrollHeight;
// 确定滚动方向
if (scrollTop >= beforeScrollTop) {
// 滚动触底
if (scrollTop + clientHeight >= scrollHeight) {
console.log("移动端滚动触底加载更多");
}
}
beforeScrollTop = scrollTop;
}
// 节流
function throttle (f, w) {
var t = null;
return function () {
if (t) {
return;
}
t = setTimeout(() => {
//执行操作(使用apply重新绑定this的指向)
f.apply(this, arguments);
t = null;
}, w);
};
}
// 监听滚动
window.onscroll = function () {
throttle(handleScroll(), 10000);
};