两种方法实现图片懒加载

懒加载的实现原理

图片的加载是由src引起的,当src赋值时,浏览器就会请求图片资源。根据这个原理,我们使用HTML5 的data-xxx属性来储存图片的路径,在需要加载图片的时候,将data-xxx中图片的路径赋值给src,这样就实现了图片的按需加载,即懒加载。

1.通过getBoundingClientRect()和window.innerHeight

流程:

  1. 获得图片列表imgList = [...document.querySelectorAll('img')]
  2. 创建懒加载自执行函数,创建空数组,把加载过的图片下标加入数组
  3. 监听滚动事件
  4. 遍历图片,获得图片相对于视口的位置。getBoundingClientRect().top
  5. 图片位置小于window.innerHeight,则更改src,getAttribute(‘’)
  6. 设置透明度
  7. 当加载完所有图片,移除监听事件document.removeEventListener('scroll', imgLazyLoad)
    <!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>Document</title>
    </head>
    <body>
        <style>
            .box{
                background-color: #DDD;
                width:200px;
                height:100px;
            }
            .box img{
                opacity: 0;
                transition: opacity 2s;
            }
        </style>
            <div class="box">
                <img src='' data-src="./1.jpg"/ style="width:200px; height:100px;">
            </div><br><br><br><br><br><br><br>
            <div class="box">
                <img src='' data-src="./1.jpg"/ style="width:200px; height:100px;">
            </div><br><br><br><br><br><br><br>
            <div class="box">
                <img src='' data-src="./1.jpg"/ style="width:200px; height:100px;">
            </div><br><br><br><br><br><br><br>
            <div class="box">
                <img src='' data-src="./1.jpg"/ style="width:200px; height:100px;">
            </div><br><br><br><br><br><br><br>
            <div class="box">
                <img src='' data-src="./1.jpg"/ style="width:200px; height:100px;">
            </div><br><br><br><br><br><br><br>
        
        <script type="text/javascript">
    
            let imgList = [...document.querySelectorAll('img')]
            let length = imgList.length
    
    
     const imgLazyLoad = (function() {
        let count = 0
        
        return function() {
            let deleteIndexList = []
            //遍历图片
            imgList.forEach((img, index) => {
                let rect = img.getBoundingClientRect()
                //出现在可视窗口时,开始加载
                if (rect.top < window.innerHeight) {
                    //更改地址
                    img.src=img.getAttribute('data-src')
                    //设置透明度
                    img.onload=()=>{
                        img.style.opacity=1
                    }  
                    deleteIndexList.push(index)
                    count++
                    //加载完所有图片之后,移除滚动条的监听事件
                    if (count === length) {
                        document.removeEventListener('scroll', imgLazyLoad)
                    }
                }
            })
            //加载过的图片从列表中移除
            imgList = imgList.filter((img, index) => !deleteIndexList.includes(index))
       }
    
    })()
    
    //监听滚动事件
    document.addEventListener('scroll', imgLazyLoad)
    
    
        </script>
    </body>
    </html>

    2,通过IntersectionObserver接口 (交叉观察器)

创建观察器:new IntersectionObserver(callback, option);

callback一般会触发两次。一次是目标元素刚刚进入视口(开始可见),另一次是完全离开视口(开始不可见)。

callback函数的参数(entries)是一个数组,被观察的对象。

用到的两个api:

IntersectionObserverEntry.target (en-US) 只读,获得可视区域内的元素

IntersectionObserverEntry.isIntersecting (en-US) 只读,返回一个布尔值, 如果目标元素与交叉区域观察者对象的根相交,则返回 true .(是否在可视区域内)

添加观察:observer()

取消观察:unobserver()

流程:

  1. 获取所有图片
  2. 创建监听器,并指定回调函数IntersectionObserver(dd)
  3. 给每个img都添加监视 observe.observe(img)
  4. 遍历回调函数中的参数(数组:被观察的对象),判断其是否在可视区域内isIntersecting,如果在,则获取该元素target
  5. 更改src,改变透明度,取消观察observe.unobserve(img)
<!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">
    <meta name="viewport" content="width=device-width, initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5"/>
    <title>Document</title>
    <style>
        img{
            opacity: 0;
            transition: opacity 2s;
        }
    </style>
</head>

<body>
    <img src="" data-src="1.jpg" style="width:200px;height:200px;">
    <br/><br/><br/><br/><br/>
    <img src="" data-src="1.jpg" style="width:200px;height:200px;">
    <br/><br/><br/><br/><br/>
    <img src="" data-src="1.jpg" style="width:200px;height:200px;">
    <br/><br/><br/><br/><br/>
    <img src="" data-src="1.jpg" style="width:200px;height:200px;">
    <br/><br/><br/><br/><br/>
    <img src="" data-src="1.jpg" style="width:200px;height:200px;">
    <br/><br/><br/><br/><br/>
    <img src="" data-src="1.jpg" style="width:200px;height:200px;">
    <br/><br/><br/><br/><br/>
    <img src="" data-src="1.jpg" style="width:200px;height:200px;">
    <br/><br/><br/><br/><br/>
    <script type="text/javascript">
       let list=document.querySelectorAll('img')
        const dd=arr=>{
            arr.forEach(item=>{
                if(item.isIntersecting){
                    let img=item.target
                    img.src=img.getAttribute('data-src')
                    img.onload=()=>{
                        img.style.opacity=1
                    }
                    observer.unobserve(img)
                }
            })
        }
       let observer=new IntersectionObserver(dd)
       list.forEach(img=>{
           observer.observe(img)
       })
    </script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值