阅读 22

图片懒加载-前端性能优化

首页加载图片过多,会造成首页加载慢以致卡顿,这时,我们可用图片懒加载的方式进行性能优化。

原理 将资源路径赋值到img标签的data-xx属性中,而不是src属性 获取img节点距离浏览器顶部的距离,如果小于或等于浏览器窗口的可视高度,那么就将data-xx的值赋值到src里去 用到的API

滚动条距离顶部高度 : document.documentElement.scrollTop || document.body.scrollTop;

可见区域高度 : seeHeight = document.documentElement.clientHeight;

代码:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        img {
            display: block;
            margin-bottom: 50px;
            width: 800px;
            height: 400px;
        }
    </style>
</head>
<body>
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg6.png" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg7.png" alt="">
    <img src="" data-src="http://ww1.sinaimg.cn/large/006y8mN6gw1fa7kaed2hpj30sg0l9q54.jpg" alt="">
    <img src="" data-src="http://cover.read.duokan.com/mfsv2/download/fdsc3/p01N203pHTU7/Wr5314kcLAtVCi.jpg!t" alt="">
    <img src="" data-src="http://77fkxu.com1.z0.glb.clouddn.com/20160308/1457402219_73571.jpg" alt="">
    <img src="" data-src="http://pic1.cxtuku.com/00/16/18/b3809a2ba0f3.jpg" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg9.png" alt="">
    <img src="" data-src="http://cover.read.duokan.com/mfsv2/download/fdsc3/p015trgKM7vw/H0iyDPPneOVrA4.jpg!t" alt="">
    <img src="" data-src="http://ww1.sinaimg.cn/large/006y8mN6gw1fa7kaed2hpj30sg0l9q54.jpg" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg8.png" alt="">
    <img src="" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg9.png" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg10.png" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg11.png" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg15.png" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg16.png" alt="">
    <img src="" data-src="https://www.mwcxs.top/static/staticImage/sliders/bg17.png" alt="">
<script>
    (function(){
    let num = document.getElementsByTagName('img').length;
    let img = document.getElementsByTagName("img");
    let n = 0; //存储图片加载到的位置,避免每次都从第一张图片开始遍历
    lazyload(); //页面载入完毕加载可是区域内的图片
     window.onscroll = lazyload;
    function lazyload() { //监听页面滚动事件
        let seeHeight = document.documentElement.clientHeight; //可见区域高度
        let scrollTop = document.documentElement.scrollTop || document.body.scrollTop; //滚动条距离顶部高度
        for (let i = n; i < num; i++) {
            // 图片未出现时距离顶部的距离大于滚动条距顶部的距离+可视区的高度
            if (img[i].offsetTop < seeHeight + scrollTop) {
                if (img[i].getAttribute("src") == "") {
                    img[i].src = img[i].getAttribute("data-src");
                }
                n = i + 1;
            }
        }
    }
    })()

</script>
</body>
</html>
复制代码
文章分类
前端
文章标签