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

相关推荐
anOnion8 小时前
构建无障碍组件之Menu Button pattern
前端·html·交互设计
用户47949283569158 小时前
claude Fable用不了?把Gpt 5.5pro接到你的claude code里
前端·后端
JieE2129 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
冬奇Lab11 小时前
AI Workflow 定义的四次演进:从 Markdown 到 JS 脚本,再到分布式多 Agent
javascript·人工智能·agent
zhangxingchao11 小时前
Kotlin常用的Flow 操作符整理
前端
IT_陈寒13 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
Pedantic14 小时前
SwiftUI 手势笔记
前端·后端
橙子家14 小时前
浏览器缓存之【结构化数据库与缓存】: IndexedDB、Cache storage 和 Storage buckets
前端
user205855615181314 小时前
X6 中边悬浮置顶,规避 `mouseleave` 事件丢失问题
前端
李明卫杭州14 小时前
CSS aspect-ratio 属性完全指南
前端