// 记录前一个滚动位置
let 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) {
let t = null;
return function () {
if (t) {
return;
}
t = setTimeout(() => {
//执行操作(使用apply重新绑定this的指向)
f.apply(this, arguments);
t = null;
}, w);
};
}
// 监听滚动
window.onscroll = function () {
throttle(handleScroll(), 10000);
};
js监听滚动到底部
最新推荐文章于 2023-12-06 12:00:00 发布