学无先后,达者为师

网站首页 前端文档 正文

JavaScript实现图片延时加载/图片懒加载/图片滚动加载

作者:五花漏 更新时间: 2022-01-09 前端文档
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片滚动懒加载</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<style>
    body {
        padding: 20px;
        width: 100%;
        text-align: center;
        background-color: #f3f3f3;
        box-sizing: border-box;
    }

    #box {
        width: 50%;
        height: 500px;
        margin: auto;
    }

    #box img {
        width: 100%;
        height: 500px;
        transition: 1s;
        opacity: 0;
    }
</style>

<body>
    <div id="box"></div>
</body>
<script>
    var data = [
        "https://img-blog.csdnimg.cn/f64f5956578e484c8e966d9f85991fd3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/534762daa9654b61a89b341a7a82e123.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/9d70c3fc65fe4e9c84e8c83771826df7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/b328014ddb92402396878c8c8a29bd76.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/a9ed8c40012a4a3ca09c1fe3dba466f8.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/fd39816f0d904ed590b3258151580fcc.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/2a44c51419064e26b017f43b2d9425df.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/93e8ab2461dc414fbd9c99abb41319db.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/051b1f4c49f343f78de32fb65d1d1882.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/334ea971d6a04aa0b3bb25d419bec2ac.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/7ce82c0979304c62a29e01c32b8c2d34.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/d6dda9d875734104895a6005027a8b42.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
        "https://img-blog.csdnimg.cn/172ca78bfda447bc95e7e29c9be6d9ee.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LqU6Iqx5ryP,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center",
    ]
    var box = document.getElementById('box');
    for (var i = 0; i < data.length; i++) {
        var img = document.createElement('img');
        img.dataset.src = data[i];
        box.appendChild(img);
    }
    var imgList = document.querySelectorAll('img');
    window.addEventListener('scroll', loadFn);
    window.addEventListener('load', loadFn);
    function loadFn() {
        let loadedImgList = []
        for (var i = 0; i < imgList.length; i++) {
            if (imgList[i].getBoundingClientRect().top < window.innerHeight) {
                if (imgList[i].dataset.src) {
                    imgList[i].src = imgList[i].dataset.src;
                    imgList[i].style.opacity = 1;
                    imgList[i].removeAttribute('data-src');
                    loadedImgList.push(i)
                }
            }
        }
        // 把已经加载过的从数组中剔除
        imgList = imgList.filter((img, index) => !loadedImgList.includes(index))
        if (imgList.length == 0) {
            // 图片都加载完,移出监听事件
            window.removeEventListener('scroll', loadFn);
        }
    }

</script>

</html>

原文链接:https://blog.csdn.net/qq_37024887/article/details/122090694

栏目分类
最近更新