当前位置:网站首页>关于图片懒加载的实现(总结梳理)
关于图片懒加载的实现(总结梳理)
2022-06-22 05:24:00 【Best_卡卡】
前言
why?
懒加载:访问一个页面,可视区域的图片优先显示, 而不是一次性加载所有图片,随可视
区域增加,再发送图片请求,避免打开网页时加载过多资源。使页面加载速度快、减轻
服务器的压力、节省流量。
实现方式:
1、监听页面监听scroll事件滚动(不推荐)
2、使用:Intersection Observer API
3、插件vue3-lazy实现(支持vue3和ts)(vue2.x可以使用vue-lazyload)
监听scroll事件
当图片距离视窗顶部的距离大于浏览器窗口显示区的高度时,图片不可见,反之可见
const images = document.querySelectorAll("img");
window.addEventListener("scroll", e => {
// 先判断每张图片的位置是否在可视区域
images.forEach(image => {
const imageTop = image.getBoundingClientRect().top;
if (imageTop < window.innerHeight) {
const data_src = image.getAttribute("data-src");
image.setAttribute("src", data_src);
}
console.log("scroll");
});
});
这种方法会多次触发,不推荐使用,推荐使用下面的IntersectionObserver
使用:Intersection Observer API
当一个IntersectionObserver对象被创建时,其被配置为监听根中一段给定比例的可见区域。
一旦IntersectionObserver被创建,则无法更改其配置,所以一个给定的观察者对象只能用来监听可见区域的特定变化值;
然而,你可以在同一个观察者对象中配置监听多个目标元素。
<template>
<main class="container">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/index/banner.png">
<img data-src="/src/assets/images/home1/ddstep2.png">
<img data-src="/src/assets/images/index/banner.png">
</main>
</template>
<script setup lang="ts">
import {
onMounted, ref } from 'vue' // introjs主题
// 传给IntersectionObserver的回调函数
// 在目标元素能看见时触发一次,目标元素看不见了时再触发一次
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const image = entry.target;
const data_src:any = image.getAttribute('data-src');
console.log('src', data_src)
image.setAttribute('src', data_src);
// 图片被加载后取消观察
observer.unobserve(image);
}
});
});
onMounted(() => {
const images = document.querySelectorAll('img');
images.forEach((image) => {
observer.observe(image);
});
})
</script>
<style scoped lang="scss">
.container{
font-family: 'Helvetica Neue';
font-style: normal;
font-weight: 500;
font-size: 24px;
line-height: 32px;
color: #171B23;
.header{
color: #434C5B;
font-family: 'PingFang SC';
}
img{
width: 200px;
height: 100px;
}
}
</style>
插件 vue3-lazy
安装
npm install vue3-lazy -S
main.ts中全局引入
import {
createApp } from 'vue'
import App from './App.vue'
import lazyPlugin from 'vue3-lazy'
createApp(App).use(lazyPlugin,{
loading: '@/assets/images/default.png', //图⽚加载中时显⽰的默认图⽚
error: '@/assets/images/error.png' //图⽚加载失败后显⽰的图⽚
})
组件使用
<img v-lazy="'/src/assets/images/index/banner.png'">
边栏推荐
- 旅行家的预算(洛谷)
- Running yolov5 reports an error attributeerror: cant get attribute sppf on module models common
- P1160 队列安排
- YARN 的高可用设计有哪些?
- 致远OA漏洞分析、利用与防护合集
- P1077 [noip2012 popularization group] flower display
- A piece of code to solve the problem of automatic disconnection of Google colab
- 从JedisSentinelPool获取jedis
- Graph calculation on nlive:nepal's graph calculation practice
- 毕业季 | 新的开始,不说再见
猜你喜欢
![P1077 [NOIP2012 普及组] 摆花](/img/0d/f74a2036aa261ed327d9d74291aacc.png)
P1077 [NOIP2012 普及组] 摆花

ActiveMQ knowledge summary of Message Oriented Middleware

Service migration when deploying SuperMap iserver war package

畢業回饋!Apache Doris 社區所有貢獻者來領禮品啦!

Kubernetes - deploy application to cluster

P1160 queue arrangement

Tupu software 2D and 2.5D case collection | smart Park, data center, SMT production line

P1160 队列安排

Topic selection system of college graduation design based on SSM
![[cloud native] 2.2 kubeadm create cluster](/img/b2/a57b7e44f74357d5aedbb9ddcd95ff.png)
[cloud native] 2.2 kubeadm create cluster
随机推荐
C#中Cookie设置与读取
rambbmitmq消费方
Hanoi Tower problem
Yarn application submission process
Contents of 2022 tea master (intermediate) examination and tea master (intermediate) examination
It's 2022. Don't you hurry to learn typescript?
P1160 队列安排
DTS migration script Oracle
Rambbmitmq consumer
What problems will be encountered during the implementation of MES management system
2022 refrigeration and air conditioning equipment operation test paper and refrigeration and air conditioning equipment operation test skills
89--- Dirac delta function
DTS迁移秘籍-SQLSERVER篇
Storage mode and lifetime of C language variables
ActiveMQ knowledge summary of Message Oriented Middleware
JS regular expression to implement the thousands separator
在Vs Code中搭建JSP开发环境
liunx虚拟机环境使用docker安装oracle数据库,并使用navicat连接
Zhiyuan OA vulnerability analysis, utilization and protection collection
C语言概念知识扫盲(不定期补充更新)