使用IntersectionObserver 實現惰性加载(demo)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .main {
            width: 50%;
        }

        .img {
            display: block;
            height: 200px;
            width: 200px;
            margin: 10px auto;
        }
    </style>
</head>

<body>
    <div class="main">
        <img class="img" src="" alt="">
        <img class="img" src="" alt="">
        <img class="img" src="" alt="">
        <img class="img" src="" alt="">
        <img class="img" src="" alt="">
        <img class="img" src="" alt="">
    </div>

</body>
<script>
    let array = [
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa2.att.hudong.com%2F27%2F81%2F01200000194677136358818023076.jpg&refer=http%3A%2F%2Fa2.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616316943&t=d642abbbc5b984f0a3591b1a1d432958',
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F52%2F62%2F31300542679117141195629117826.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616316943&t=929bb81f04613d98a6dfcd423408a6ca',
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa3.att.hudong.com%2F35%2F34%2F19300001295750130986345801104.jpg&refer=http%3A%2F%2Fa3.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616316974&t=496647ffa3a424fabac8648902c7f182',
        'https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/9c16fdfaaf51f3de9ba8ee1194eef01f3a2979a8.jpg',
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.aiimg.com%2Fuploads%2Fuserup%2F0909%2F2Z64022L38.jpg&refer=http%3A%2F%2Fimg.aiimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616316974&t=2063f669ff6a25f7063dee2b0c6e97ec',
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa3.att.hudong.com%2F92%2F04%2F01000000000000119090475560392.jpg&refer=http%3A%2F%2Fa3.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616316974&t=87bfda72fcc5ba77e090052b93a8eff1'
    ]
    for (let i = 0; i < array.length; i++) {
        var observer = new IntersectionObserver(
            function (changes) {
                changes.forEach(function (change) {
                    var container = change.target;
                    // 相交率,默认是相对于浏览器视窗
                    if (change.intersectionRatio > 0) {
                        container.src = array[i]
                        observer.unobserve(container);
                    }
                });
            }
        );
        observer.observe(document.querySelectorAll('.img')[i]);
    }
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值