js 函数的多图片懒加载(lazy) 带插件版完整解析

前言:

        本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽。

        本篇文章为您分析一下原生JS实现图片懒加载效果

页面需求

 1. 在用户刚刚进入页面时,先加载首屏的所有图片,如果用户还想看更多的图片,当用户滑动滚动条时再加载出现在视口内的图片。

HTML结构


    <div class="lazy">
        <img src="default.png" alt=""
            data-src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=148443545,806875479&fm=26&gp=0.jpg">
        <img src="default.png" alt=""
            data-src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3001021586,810308097&fm=26&gp=0.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786712&di=ffcad1073ba07e3af701348e14613cc2&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn01%2F246%2Fw640h406%2F20180904%2Fd41c-hiqtcan7167756.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786712&di=f68274504cf3e1d9a8f32d19bc305154&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2Fw640h426%2F20180112%2F69ae-fyqnick8791762.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786711&di=049d837a5c9d2dc98ff53c48d4557160&imgtype=0&src=http%3A%2F%2Fpicture.ik123.com%2Fuploads%2Fallimg%2F141031%2F17-141031160326.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786710&di=f87e22bfe5da4ea2d088519f8958816f&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ffront%2F331%2Fw870h1061%2F20180915%2FK-2y-hikxxna1448280.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786709&di=bc67716679d7234d4082886458a5d8cb&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F65%2F01%2F19300001035357130795018787137_950.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786708&di=7ba455ddcd0537f5ef51c8fff325e865&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201511%2F24%2F20151124145437_w4dVB.jpeg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786707&di=9fb358257a48fe9c79c72c4fc656e6b1&imgtype=0&src=http%3A%2F%2F00.minipic.eastday.com%2F20160906%2F20160906153420_f9b85f408460d00488f5f9e813b7e2a7_6.jpeg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786804&di=64c75d1d8ecf82693e58766e8b606873&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fforum%2Fpic%2Fitem%2Fcd5bb743ad4bd1136235f0ce5aafa40f49fb059b.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786804&di=e53121cfa8f2448ee5758372d9f80a45&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fq_70%2Cc_zoom%2Cw_640%2Fimages%2F20180512%2F50ad001e559b4eb1b9c22aca65b0640d.jpeg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786803&di=32564289c9ffb12d0747d122cf82751b&imgtype=0&src=http%3A%2F%2Fpic25.photophoto.cn%2F20121021%2F0035035953679533_b.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786803&di=065a222054f9aa35a6d80214c12bd210&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2Fw562h464%2F20180119%2F8f01-fyqtwzu8355015.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588786802&di=53c06ed741f89e6ebf92a8f60c0eae57&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn17%2F307%2Fw640h467%2F20180709%2F6458-hezpzwu1922800.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958074&di=d09f2d50db3a63d0d5a3b5484dcaf2ba&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2Fw558h559%2F20180211%2Fe4bf-fyrkuxt5566269.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958074&di=ab6c49cc5a1be598a1fa279a36a3e817&imgtype=0&src=http%3A%2F%2Fpic.makepolo.net%2Fnews%2Fallimg%2F20161228%2F1482940135505940.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958073&di=9a19d89171e8585e8ed5f19dbab33149&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn13%2F56%2Fw499h357%2F20181114%2Fb7e5-hnvukfe8184806.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958073&di=d81b5a7c3a09ce927c35c753251a3175&imgtype=0&src=http%3A%2F%2Fp3.ssl.cdn.btime.com%2Ft01c26630ad64f0175c.png%3Fsize%3D640x572">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958072&di=bf97ec847c6c10e4a0f3986dafca641b&imgtype=0&src=http%3A%2F%2Fupload.northnews.cn%2F2016%2F0905%2Fthumb_900_600_1473046880257.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958072&di=b445562cc398823b2fd859eec5312ba3&imgtype=0&src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20181025%2F21%2F1540472811-DTyBFexQio.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958069&di=64a0b81403b9a96b9cd3b4da5c2cd96b&imgtype=0&src=http%3A%2F%2Fphotocdn.sohu.com%2F20141225%2Fmp620837_1419522163024_10.jpeg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958066&di=dde85061135c43dc536cc983fa0e557e&imgtype=0&src=http%3A%2F%2Fwww.xiao89.com%2Fuploads%2Fallimg%2F190605%2F10-1Z605154546.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958066&di=956eb9de655a8879bcf015a953892104&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ffront%2F480%2Fw640h640%2F20180920%2FrTAl-hikxxna3627881.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586588958065&di=88d01b1a1134ada3b601eb2c733379d3&imgtype=0&src=http%3A%2F%2Fimg.boqiicdn.com%2FData%2FBK%2FA%2F1811%2F2%2Fimg60655bdbcdef8b269_o.jpg">
        <img src="default.png" alt=""
            data-src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586589478110&di=56192deca9c7e85508b2075e91ad55b2&imgtype=0&src=http%3A%2F%2Fcdn6.hbimg.cn%2Fstore%2Fwm%2Fbigfiles%2F201351%2FD52586DA90B7D8B4D4A8CBC334.jpg">
    </div>

CSS样式


        .lazy {
            position: relative;
            text-align: center;
        }

        .lazy img {
            width: 327px;
            height: 275px;
        }

JS行为

JS大致思路
 1. 获取滚动条高度      

 2. 获取浏览器视口的高度

 3. 循环每一张图片并获取到每一张图片的高度偏移量

 4. 如果当前的图片偏移量 < 滚动条的位置高度 + 浏览器窗口高度  (能出现在视口,我们正常能看见的区域)

 5. 如果不是下载完再执行的情况

 6. 根据图片的data-src属性设置图片的真实路径

        var dom = document.querySelectorAll(".lazy img"); // 获取所有的图片
        function lazy() {
            var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; // 获取滚动条的高度
            var windowHeight = window.innerHeight;  // 获取视口高度
            for (var i = 0; i < dom.length; i++) {
                var imgHeight = dom[i].offsetTop;  // 每张图片的偏移量
                if (imgHeight < scrollTop + windowHeight) {  // 判断
                    (function (j) {
                        setTimeout(function () {  // 用一个定时器,延迟多少秒后再渲染图片
                            var newImage = new Image();  // 创建图片对象
                            newImage.src = dom[j].getAttribute("data-src");  // 图片存储临时空间
                            newImage.onload = function() {
                                dom[j].src = dom[j].getAttribute("data-src"); // 设置图片的正确路径
                            }
                        },2000);
                    })(i)
                }
            }
        }
        // 当滑动滚动条时
        window.onscroll = lazy;
        lazy();

这样图片的懒加载效果就完成了。下面来看一看插件版的使用方式

插件版JS代码 (lazy.js)


if (!window.myPlugin) {
    window.myPlugin = {};
}

/**
 * 图片懒加载
 * @param { Object } dom 传入需要懒加载的图片元素
 * @param { Number } time 加载需要的时间
 */
window.myPlugin.lazy = function (dom,time) {
    // 获取滚动条的位置高度 兼容处理 谷歌浏览器只认识document.body.scrollTop;  其他浏览器document.documentElement.scrollTop;
    var scrollHeight = document.body.scrollTop || document.documentElement.scrollTop;
    // 获取浏览器窗口高度  包含下面的滚动条高度
    var windowHeight = window.innerHeight;
    // 循环获取到每张图片
    for (var i = 0; i < dom.length; i++){
        // 获取每张图片距离页面上方的偏移量   注意offset的一些坑,offset并不是子元素相对于父元素的,而是相对于定位元素
        var imgHeight = dom[i].offsetTop;
        // 如果当前的图片偏移量 < 滚动条的位置高度 + 浏览器窗口高度  (能出现在视口,我们正常能看见的区域)
        if (imgHeight < scrollHeight + windowHeight) {
            (function (i) { // 此处为啥要使用立即执行函数,因为setTimeout是异步执行的
                setTimeout(function () {
                    var newImage = new Image();                       // 创建原始图片对象
                    newImage.src = dom[i].getAttribute("data-src");   // 图片临时存储空间
                    newImage.onload = function () {                   // 当图片资源加载完成后
                        dom[i].src = dom[i].getAttribute("data-src"); // 设置图片的正确路径
                    }
                },time);
            })(i);
        }
    }
    // 滚动时再次加载
    window.onscroll = function () {
        this.myPlugin.lazy(dom,time);
    }
}

使用时这样使用

    <script src="../../plugin/lazy.js"></script>
    <script>
        var dom = document.querySelectorAll(".lazy img")
        this.myPlugin.lazy(dom,2000);
    </script>

结语

整完!

上一篇:OCR技术浅探:基于深度学习和语言模型的印刷文字OCR系统


下一篇:kernel 4.4.12 EETI eGTouch 电容屏驱动移植