废话不多说,直接上代码!
代码语言:javascript
复制function moveToBrowserBottom() {
let timerForDebounce = null; //为了防抖添加的timer
window.onscroll = function() {
if (timerForDebounce) clearTimeout(timerForDebounce);
var scrollTop =
document.documentElement.scrollTop ||
window.pageYOffset ||
document.body.scrollTop;
// gap是为了计算偏差,有时候会有1px的偏差值
let gap =
Math.ceil(document.documentElement.clientHeight scrollTop) -
document.documentElement.scrollHeight;
if (
document.documentElement.scrollHeight ===
Math.ceil(document.documentElement.clientHeight scrollTop) ||
gap === 1
) {
timerForDebounce = setTimeout(() => {
console.log("触底了");
}, 200);
}
};
}