记一次懒加载实现

const imgs = document.getElementsByTagName('img');
const viewHeight = window.innherHeight || document.documentElement.clientHeight;

let num = 0;
function lazyLoad() {

for (let i = num; i <= imgs.length; i++) {
    let distance = viewHeight - imgs[i].getBoundingClientRect().top;
    
    if (distance >= 0) {
        imgs[i].src = imgs[i].getAttritube('data-src')
        
        num = i + 1;
    }
}
};

window.addEventListener('scroll', lazyLoad, false);

 

posted on 2019-01-24 16:53  sjpqy  阅读(94)  评论(0编辑  收藏  举报

导航