scrollTop:滾動條滾動時,滾動條上端距離頂部的距離
clientHeight:可視區(qū)的高度
scrollHeight:滾動條的總高度(當(dāng)前可滾動的頁面的總高度)
window.addEventListener('scroll', scroll)
function scroll() {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
const clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
if (scrollTop === 0) {
console.log('滾動到頂了');
}
if (scrollTop + clientHeight >= scrollHeight) {
console.log('滾動到底了');
}
}
window.removeEventListener('scroll', scroll) // 移除滾動事件
在部分瀏覽器上妙色,到底部時桅滋,觸發(fā)不了
可以考慮:
1.用offsetHeight代替clientHeight ,多計算邊框的距離
2.scrollTop的實時監(jiān)聽值可能是小數(shù)身辨,采用Math.ceil(scrollTop),向上取整虱歪,避免少那個零點幾的距離