图片懒加载(lazyload )

news2024/11/27 18:36:36

图片懒加载

懒加载(Lazy Loading)是一种计算机编程技术,用于延迟初始化对象或资源,直到它们实际需要使用时才进行加载或初始化。这种技术可以提高程序的启动速度和性能,减少不必要的资源消耗,特别是在处理大型数据集或资源密集型任务时。

图片懒加载

1.使用intersectionObserver(交叉观察器)处理图片加载问题

相关链接:IntersectionObserver介绍
代码部分
html部分

<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>

  <img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
  <img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
  <img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
  <img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
  <img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
  <p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
    computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>

js部分

 <script>
    // 获取图片dom
    let images = document.querySelectorAll('img');

    // 创建intersectionObserver实例
    const imagesObserver = new IntersectionObserver(entries => {
      entries.forEach(entry => {
        // 1.判断图片是否显示
        if (entry.isIntersecting) {
          // 2.获取原img的src内容
          const image = entry.target;
          const data_src = image.getAttribute('data-src');
          // 3.设置其src属性
          image.setAttribute('src', data_src)
          // 4.取消观察
          imagesObserver.unobserve(image);
        }

      })
    })

    // 观察所有图片
    images.forEach(item => {
      imagesObserver.observe(item);
    })
  </script>

封装的lazyload.js

// 1.获取图片集合
const images = document.querySelectorAll('img')
// 2.实例intersectionObserver
const imagesObserver = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const image = entry.target;
      const data_src = image.getAttribute('data-src');
      image.setAttribute('src', data_src);
      imagesObserver.unobserve(image)
    }
  })
})
// 3.为图片创建实例
images.forEach(item => {
  imagesObserver.observe(item)
})
2.使用img标签中的loading属性设置成为lazy,注意的时,该属性在一些浏览器中不兼容。

在这里插入图片描述

  <img loading="lazy" src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">

3.添加通过监听滚动事件进行处理
window.addEventListener('scroll', lazyLoadImages);
function lazyLoadImages() {
  const images = document.querySelectorAll('img[data-src]');
  images.forEach(image => {
    if (image.getBoundingClientRect().top < window.innerHeight) {
      image.src = image.getAttribute('data-src');
    }
  });
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2206851.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于头脑风暴优化的模糊PI控制系统simulink建模与仿真

目录 1.课题概述 2.系统仿真结果 3.核心程序与模型 4.系统原理简介 5.完整工程文件 1.课题概述 头脑风暴优化&#xff08;Brain Storm Optimization, BSO&#xff09;是一种受人类集体创新过程启发的群体智能算法。它通过模拟团队成员之间的信息交流和想法生成来寻找最优解…

【含开题报告+文档+PPT+源码】基于SpringBoot+Vue医药知识学习与分享平台的设计与实现

开题报告 本论文介绍了一个名为岐黄之家的知识学习与分享平台的设计与实现。该平台旨在为用户提供一个交流、学习和分享医药知识的空间。论文首先介绍了中医院交流平台的背景和相关研究现状。随着互联网的快速发展&#xff0c;中医学的学习和交流需求逐渐增多&#xff0c;因此…

77.【C语言】EOF的解释

1.cplusplus网的介绍 在这几篇文章提到过,但没有详细阐释过EOF的细节 24.【C语言】getchar putchar的使用E4.【C语言】练习&#xff1a;while和getchar的理解32.【C语言】详解scanf 75.【C语言】文件操作(3) cplusplus网的介绍 点我跳转 翻译 常量 EOF 文件结束(End-Of-Fi…

新版Win32高级编程教程-学习笔记01:应用程序分类

互联网行业 算法研发工程师 目录 新版Win32高级编程教程-学习笔记01&#xff1a;应用程序分类 控制台程序 强烈注意 窗口程序 启动项 程序入口函数 库程序 静态库 动态库程序 几种应用程序的区别 控制台程序 本身没有窗口&#xff0c;其中的doc窗口&#xff0c;是管…

大数据之——VWare、Ubuntu、CentOs、Hadoop安装配置

前言&#xff1a;这里很抱歉前几期考研专题以及PyTorch这些内容都没有更新&#xff0c;并不是没有在学了&#xff0c;而是事太鸡儿多了&#xff0c;前不久刚刚打完华为开发者比赛&#xff0c;然后有紧接着高数比赛、考研复习&#xff0c;因此这些后续文章都在草稿状态中&#x…

yolov8-melodic-cam-anconda环境配置及目标检测

1、基础环境安装 安装配置cuda、Anconda等环境&#xff0c;具体安装参考如下&#xff1a; https://blog.csdn.net/weixin_45702256/article/details/142555187 2、torch安装 下载链接&#xff1a;https://pytorch.org/ 根据配置下载对应版本&#xff0c;CUDA11.4 可用11.3下…

【c数据结构】队列详解!(模拟实现、OJ练习实操)

队列的概念 队列就像排队&#xff0c;先进先出&#xff0c;zz先到先得&#xff08;队头的人先出去&#xff0c;队尾的人排在最后出去&#xff09; 对比栈 队列示意图 概念&#xff1a;只允许在⼀端进⾏插⼊数据操作&#xff0c;在另⼀端进⾏删除数据操作的特殊线性表&#xff…

68 Netty

68 Netty 参考资料 【硬核】肝了一月的Netty知识点 概念 Netty 是一个高性能、异步事件驱动的网络应用框架&#xff0c;简化了 Java 网络编程&#xff0c;适用于构建高效、可扩展的网络服务器和客户端。 Netty 是基于 Java NIO 的异步事件驱动的网络应用框架&#xff0c;使…

访问远程桌面或共享文件夹,输入正确凭证,但提示登录没有成功或者用户名密码不正确

可以在目标机器试试以下方法&#xff1a; winR 打开 "gpedit.msc" 本地组策略编辑器&#xff0c;导航到 计算机配置 > Widnows 设置 > 安全设置 > 本地策略 > 安全选项 找到 网络访问&#xff1a;本地账户的共享和安全模型&#xff0c;把 仅来宾 改为 …

介绍各种编程语言

记得点个赞再看哦 常见的编程语言 在当今的计算机编程领域&#xff0c;有许多种编程语言&#xff0c;以下是一些常见的编程语言&#xff1a; Python&#xff1a;是一种代表简单思想的语言&#xff0c;具有极其简单的语法&#xff0c;是FLOSS&#xff08;自由/开放源码软件&…

简单解析由于找不到xinput1_3.dll,无法继续执行代码的详细解决方法

电脑上突然跳出“由于找不到xinput1_3.dll&#xff0c;无法继续执行代码”的提示&#xff0c;这着实令人心烦&#xff0c;特别是当你正着急使用相关软件或者程序的时候。别担心&#xff0c;其实有五种科学有效的解决办法。大家得清楚&#xff0c;xinput1_3.dll是一个在众多软件…

快速总结HAT

HAT&#xff1a;Activating More Pixels in Image Super-Resolution Transformer 图像恢复(Image restoration)是计算机视觉中的一个经典问题&#xff0c;它的目的是从给定的低质量&#xff08;LQ&#xff09;输入重建高质量&#xff08;HQ&#xff09;图像。 在SwinIR基础上…

Codeforces 977 Div2 A-B

课上找空做了 A 原题 A. Meaning Mean 思路 贪心即可 代码 #include <bits/stdc.h> #define int long long using namespace std;const int N 110;int n, m, k, x, y, z; int w[N], f[N];void solve() {cin >> n;for (int i 1; i < n; i )cin >>…

理解计算机系统_程序的机器级表示(特别篇):对比move和leaq看汇编语言中数据的传递,变量和指针的映射,指针的实现

前言 以<深入理解计算机系统>(以下称“本书”)内容为基础&#xff0c;对程序的整个过程进行梳理。本书内容对整个计算机系统做了系统性导引,每部分内容都是单独的一门课.学习深度根据自己需要来定 引入 汇编代码传递数据的机制令人费解.而这部分内容又是比较基础的,需要…

自适应气泡小计

View 代码 <div class"mas-view"><div class"m-2"><span>新新消息消新消息消息新消息新消息新消息新消息新消息新消息新消息新消息新息</span></div></div>CSS 样式 .mas-view{width: 100%;height: auto;border: 1px…

[M数学] lc3164. 优质数对的总数 II(因数分解+倍增+推公式+思维+好题)

文章目录 1. 题目来源2. 题目解析 1. 题目来源 链接&#xff1a;3164. 优质数对的总数 II 2. 题目解析 挺不错的一道 因数分解、倍增 的题目&#xff0c;需要一定的思维和推公式的能力才能解决。灵神的题解已经非常清晰易懂了&#xff0c;可以直接去看。 倍增思路&#xff…

Kafka 消费者位置提交方式及适用场景

在使用 Kafka 进行消息处理时&#xff0c;消费者的位置提交是一个非常重要的环节。它决定了消费者在下次启动时从哪里开始读取消息。今天&#xff0c;我们就来深入探讨一下 Kafka 消费者位置提交方式有哪些&#xff0c;以及在什么场景下使用。 一、Kafka 消费者位置提交的重要…

Linux的zookeeper安装部署

1.zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是hadoop和HBASE的重要组件 2.下载zookeeper安装包zookeeper安装包https://archive.apache.org/dist/zookeeper/zookeeper-3.5.9/ 移动到Linux解压 解压到/export/server文件夹 命令: tar -xvf apache-zooke…

《Linux从小白到高手》综合应用篇:详解Linux系统调优之内存优化

本篇介绍Linux服务器系统内存调优。 内存是影响Linux性能的主要因素之一&#xff0c;内存资源的充足与否直接影响应用系统的使用性能。内存调优的主要目标是合理分配和利用内存资源&#xff0c;减少内存浪费&#xff0c;提高内存利用率&#xff0c;从而提升系统整体性能。 1.内…

选择智能工单系统的理由,功能与效益分析

智能工单管理系统提升企业客户服务效率和质量&#xff0c;具备多渠道接收、智能分配、自动化处理等功能。ZohoDesk等系统通过实时响应、数据分析等优化服务流程&#xff0c;成为企业提升竞争力的关键工具。 一、智能工单管理系统的概念与优势 1. 智能工单管理系统概念 智能工…