之前用Vue制作知乎日報(bào),因?yàn)樾侣剶?shù)據(jù)過多,不會(huì)一次性加載全部。所以采用滑動(dòng)到底部之后再加載更多的新聞
JS實(shí)現(xiàn)思路
實(shí)現(xiàn)條件:窗口的高度(clientHeight)+ 滾動(dòng)的高度(scrollTop)=> 元素內(nèi)容的總高度(scrollHeight)
總體:document.documentElement.scrollTop + document. body.clientHeight => document.body.scrollHeight
function scb(){
let st = document.documentElement.scrollTop || document.body.scrollTop;
let ch = document.body.clientHeight;
let sh = document.body.scrollHeight;
if(st + ch == sh) {
console.log('it is bottom')
}
}
window.addEventListener('scroll',scb);
效果截圖

滑動(dòng)到底部事件