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

相关推荐
Js_x5 小时前
从 200 QPS 到 5 万 QPS:一次全链路性能优化实录(含压测数据、架构演进图、8 层瓶颈逐一击破)
性能优化·架构
星栈6 小时前
我在浏览器里试了试 PhotoCraft|Rust + WASM 修图,是玩具还是生产力?
前端
雨中飘荡的记忆7 小时前
淘宝搜不到的东西,能不能去全世界找?一个程序员的创业尝试
前端·设计
kyriewen7 小时前
我让 4 位 AI 面试官打分,同一个我差了 40 分
前端·面试·程序员
Star4zure8 小时前
Lossless Scaling 最新游戏画面增强实用教程
人工智能·性能优化·小黄鸭·lsfg·智能补帧·无损放大·游戏帧率
htzyl2068 小时前
Vue3中watch的deep默认值:reactive与ref为什么相反
前端·vue.js
用户112936112198 小时前
没写样式为什么还找我要 wxss?uni-app 这个 ENOENT 坑多数人误判了
前端·微信小程序·uni-app
用户553665935269 小时前
从零做一个全国山峰与线路库:山野抽签的初衷、架构与踩坑
前端
夕水9 小时前
用 SolidJS 复刻 DeepSeek Chat —— 顺便把 ew-solid-ui 拷打了一遍
前端·javascript
CCblandy9 小时前
尾延迟请求对冲实测:20ms阈值让p99降至54–56ms,代价是约60%冗余流量
python·性能优化·架构·分布式系统·尾延迟