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去详细查看!!!

相关推荐
weixin_4643076331 分钟前
设置程序自启动
前端
小满zs36 分钟前
Next.js第十七章(Script脚本)
前端·next.js
小满zs2 小时前
Next.js第十六章(font字体)
前端·next.js
喝拿铁写前端7 小时前
别再让 AI 直接写页面了:一种更稳的中后台开发方式
前端·人工智能
A向前奔跑8 小时前
前端实现实现视频播放的方案和面试问题
前端·音视频
十一.3668 小时前
131-133 定时器的应用
前端·javascript·html
xhxxx9 小时前
你的 AI 为什么总答非所问?缺的不是智商,是“记忆系统”
前端·langchain·llm
wtrees_松阳9 小时前
【弦断九章·CPU篇】鸿蒙应用性能优化心法
华为·性能优化·harmonyos
38242782710 小时前
python:输出JSON
前端·python·json