使用IntersectionObserver让html滚动到可见区域图片懒加载

46 篇文章 0 订阅
41 篇文章 1 订阅
本文介绍了一种使用IntersectionObserver API实现的图片懒加载技术,通过监听DOM元素的可见性来动态加载图片,有效提升网页加载速度和用户体验。文章详细展示了如何通过JavaScript代码实现这一功能,并提供了完整的HTML和CSS样式代码。
摘要由CSDN通过智能技术生成
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <title>使用IntersectionObserver实现懒加载</title>
 
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        ul,li {
            list-style: none;
        }
        .list {
            width: 800px;
            margin: 0 auto;
        }
        .list ul {
            width: 100%;
            overflow: hidden;
        }
        .list ul li {
            float: left;
            width: 185px;
            height: 400px;
            margin-bottom: 10px;
            margin-left: 10px;
            background-color: #ccc;
            overflow: hidden;
            text-align: center;
            line-height: 400px;
            color: red;
            font-size: 24px;
        }
 
    </style>
</head>
<body>
 
<div class="list">
    <ul>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
        <li class="lazy-loaded">
            <template>
                <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
            </template>
            <span class="loading">正在加载...</span>
        </li>
    </ul>
</div>
 
</body>
<!--  -->
<script type="text/javascript">
    //获取dom
    function filterDom(selector) {
        //
        return Array.from(document.querySelectorAll(selector));
    }
    //事件观察者
    var observer = new IntersectionObserver(observerCall,{
        root: null,
        rootMargin : '0px 0px 20px 0px',
        threshold:[0, 1]
    });
 
    function observerCall(changes) {
        changes.forEach(function(change) {
            setTimeout(function(){
                if(change.intersectionRatio > 0){
                    var container = change.target;
                    var content = container.querySelector('template').content;
                    container.appendChild(content);
                    container.querySelector('.loading').style.display = 'none';
                    observer.unobserve(container);
                }
 
            }, 100);
        });
    }
    //过滤元素
    filterDom('.lazy-loaded').forEach(function (item) {
        observer.observe(item);
    });
</script>
</html>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值