移动端懒加载js模块

function load(){
    // 获取页面已经滚动的高度
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    // 获取当前屏幕的高度
    var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
    // console.log(scrollTop);
    // console.log(clientHeight);

    //获取图片的类名
    var image = document.getElementsByClassName("images");

    // 循环遍历
    for(var img of image){
        // console.log(img);
        //判断 如果图片.offsetop<= 滚动高+屏幕高
        if((img.scrollTop + 200 <= (scrollTop + clientHeight))){
            var realSrc = img.getAttribute('data-realSrc');
            // console.log(realSrc);
            img.src = realSrc;
        }
    }


}
window.onload = load;
window.onscroll = load;
 
 

 

 

posted @ 2022-05-30 20:29  杀猪饲料  阅读(52)  评论(0)    收藏  举报