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

46 篇文章 0 订阅
41 篇文章 1 订阅
<!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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值