首页 > 其他分享 >三种懒加载方式及对比

三种懒加载方式及对比

时间:2023-02-14 23:11:28浏览次数:29  
标签:box function img let lazyLoad 三种 document 对比 加载

利用getBoundingClientRect

  • 优点:容易理解,操作easy

  • 缺点:只能用于全屏滚动的列表,不适用于局部滚动,譬如下图

snipaste_20230214_225429.png


<!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>
    <style>
        #box{
            height: 300px;
            overflow: scroll;
        }
        img {
            width: 100%;
        }
    </style>
</head>

<body>
    <div id="box">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
    </div>
</body>
<script>
    window.onload = function () {
        let box = document.getElementById('box')
        let img = [...document.querySelectorAll('img')]
        box.onscroll=function(){
            lazyLoad()
        }
        lazyLoad()
        function lazyLoad(){
            for(let i = 0;i<img.length;i++){
                let {top,bottom} = img[i].getBoundingClientRect()
                // 判断是否在box显示界限内
                if(top>0&&bottom<box.offsetHeight+img[i].offsetHeight/2){
                    console.log(top,bottom)
                    img[i].src = img[i].dataset.src
                    img.splice(i,1)
                    i--
                }
            }
        }
    }
</script>

</html>

利用api IntersectionObserver

优点:容易使用,适用于各种场景

缺点:兼容性不是很好

<!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>
    <style>
        #box{
            height: 300px;
            overflow: scroll;
        }
        img {
            width: 100%;
        }
    </style>
</head>

<body>
    <div id="box">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
    </div>
</body>
<script>
    window.onload = function () {
        let box = document.getElementById('box')   
        let img = document.querySelectorAll('img')
        let io = new IntersectionObserver(function (entries) {
            entries.forEach(entry => {
                console.log(entry.intersectionRatio)
                if (entry.intersectionRatio > 0) {
                    entry.target.src = entry.target.dataset.src
                    io.unobserve(entry.target)
                }
            })
        })
        img.forEach(item => {
            io.observe(item)
        })
    }
</script>

</html>

借用offsetTop

  • 优点:适用各种情况
  • 缺点:需要对原生代码熟悉,过后容易忘记
<!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>
    <style>
        #box{
            height: 300px;
            overflow: scroll;
            position: relative;
        }
        img {
            width: 100%;
        }
    </style>
</head>

<body>
    <div style="height: 300px;width: 200px;"></div>
    <div id="box">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
        <img src="./213.jpg"
            data-src="https://img1.baidu.com/it/u=2490614914,3569829953&fm=253&fmt=auto&app=138&f=JPEG?w=591&h=500"
            alt="">
    </div>
</body>
<script>
    window.onload = function () {
        let box = document.getElementById('box')
        let img = [...document.querySelectorAll('img')]
        box.onscroll=function(){
            lazyLoad()
        }
        lazyLoad()
        function lazyLoad(){
            for(let i = 0;i<img.length;i++){
                // 记得父元素需要设置定位
                let offsetTop = img[i].offsetTop
                if(box.scrollTop+box.offsetHeight/2>=offsetTop){
                    img[i].src = img[i].dataset.src
                    img.splice(i,1)
                    i--
                }
            }
        }
        
    }
</script>

</html>

标签:box,function,img,let,lazyLoad,三种,document,对比,加载
From: https://www.cnblogs.com/axu1997/p/17121186.html

相关文章

  • m基于功率谱,高阶累积量和BP神经网络-GRNN网络的调制方式识别仿真,对比2psk,4PSK,2FSK
    1.算法描述首先区分大类的话采用的基于功率谱提取的len参数(峰值频率间隔),用峰值个数来代替,这样能很好的区分大类把MFSK和MPSK信号区分开。针对MPSK:一:基于瞬时参数——Char......
  • m基于功率谱,高阶累积量和BP神经网络-GRNN网络的调制方式识别仿真,对比2psk,4PSK,2FSK
    1.算法描述       首先区分大类的话采用的基于功率谱提取的len参数(峰值频率间隔),用峰值个数来代替,这样能很好的区分大类把MFSK和MPSK信号区分开。 针对MPSK: ......
  • 谷歌系平台和外贸B2B平台对比
    今天,我们将以问答的形式帮助您更深入地理解这个话题。1)为什么国外B2B平台很少?只有中国和印度是B2B最受欢迎的国家,其他国家没有像样的B2B,原因如下:▼有些国家工业生产和国际贸......
  • 创建线程的三种基本方式
    多线程的基本概念,线程创建的三种基本方式及线程的生命周期等其他线程相关的简要介绍Author:MsuenbDate:2023-02-14多线程基本概念程序(program):为完成特定任务......
  • PHP上传大文件的三种解决方案
    ​PHP用超级全局变量数组$_FILES来记录文件上传相关信息的。1.file_uploads=on/off 是否允许通过http方式上传文件2.max_execution_time=30 允许脚本最大执行时间......
  • Python 识别二维码(三种方案,识别成功有两种)
    Python识别二维码(三种方案,识别成功有两种)背景python扫码方案方案一:opencv识别二维码-失败先查看一下自己的opencv版本:pipfreeze|grepopencv如果什么都没有输......
  • JSP上传大文件的三种解决方案
    ​ 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现。先说下要求:PC端全平台支持,要求支持Windows,Mac,Linux......
  • Web上传大文件的三种解决方案
    ​ 前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对Http协议较模糊,故这次采用渐进的方式来学习文件上传的......
  • 类加载过程
      加载过程其中验证,准备,解析合称链接加载通过类的完全限定名,查找此类字节码文件,利用字节码文件创建Class对象.验证确保Class文件符合当前虚拟机的要求,不会危害......
  • app直播源码,接口在5秒之后未返回数据,自动加载百分比
    app直播源码,接口在5秒之后未返回数据,自动加载百分比   //定义变量  data(){    return{      timeOutObj:{},      indexTim......