JavaScript---lazyload图片懒加载处理_IntersectionObserver

IntersectionObserver API兼容性:

部分代码展示:

html 复制代码
  // 懒加载处理
    const imgsElem = document.querySelectorAll('img');
    const topElem = document.querySelector('#top');
    // IntersectionObserver
    const Observer = new IntersectionObserver((entries, observer) => {
      entries.forEach((entry) => {
        if (entry.isIntersecting) {
          let img = entry.target;
          img.src = img.dataset.src
          observer.unobserve(img)
        }
      })
    })

    // 绑定监听API
    imgsElem.forEach((img) => {
      Observer.observe(img)
    })

具体代码请点击InsCode去详细查看!!!

相关推荐
java1234_小锋2 小时前
【技术专题】HTML5 网页技术 - HTML5 Canvas 基础以及动画入门
前端·html·html5
知了学历孙老师6 小时前
小自考的校考和统考,出题方和难度差在哪
java·大数据·前端
ShineWinsu8 小时前
对于JavaScript:操作元素的解析
开发语言·前端·javascript·面试·web·dom·元素
福兮说8 小时前
JSON 格式正确,数据就可靠吗?我用一份自编配置检查类型与空值
java·前端·json
程序员老赵10 小时前
Docker 部署青龙面板:轻松搭建定时任务管理平台
前端·docker·开源
Swot10 小时前
Asciidoctor 公式数字下标错位问题:原因分析与解决方案
前端
Swot10 小时前
leo-editor 中将 Markdown 内容转成 asciidoc 格式
前端
狒狒AI10 小时前
Remotion / GSAP+Three / HyperFrames / Skia 四栈实测:代码渲染知识视频的选栈方法(附开源技能)
前端·人工智能·aigc
修炼的dance10 小时前
浏览器里导出H.265,8.5秒和55.5秒差在硬件
前端
补码缩写10 小时前
WebCodecs选编码器,探测之后还要验解码和播放
前端