当前位置:网站首页>Lazy loading scheme of pictures
Lazy loading scheme of pictures
2022-07-05 10:44:00 【Code Bruce Lee】
Image lazy loading scheme
Preface
Lazy image loading is one of the important aspects of front-end performance improvement , Its main purpose is : Reduce network load , Enhance the experience .
But in order to influence the experience , You need to write the best layout style , It's best to write a bitmap .
Principle of lazy loading
In fact, the loading of pictures is mainly due to the src Caused by the , And if we want to implement lazy loading , Just avoid src Load the path , We can store the path of the picture to data-src On the user-defined data attribute , Then load on demand when needed .
There are two ways to implement on-demand loading : Here we focus on the first , At present, it is on the mobile end and pc Most of the waterfall flow patterns are the first .
1 Scroll to the bottom of the page to load
2 Paging load
Scroll to the bottom of the page to load the principle
The original way : Rolling monitoring
Its main principle is to monitor rolling events , When the scroll bar height is found + The height of the scroll bar is equal to document Altitude time , Is to reach the bottom of the page , When you need to reload pictures .
KaTeX parse error: Expected '}', got 'EOF' at end of input: …ction(){ if((window).scrollTop() + ( w i n d o w ) . h e i g h t ( ) > = (window).height()>= (window).height()>=(document).height()){
var $div = ‘
- append Added node
- ’;
KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲container').app…div)
}
});
A new way :IntersectionObserver
Let's go check it out jq Of lazyload How to write this part of the function in the source code of , The core is dependence IntersectionObserver, Is to detect whether the element enters the view , If there's access , Then it will be removed lazy Of class, Its source code address : link
document.addEventListener(“DOMContentLoaded”, function() {
var lazyImages = [].slice.call(document.querySelectorAll(“img.lazy”));
if (“IntersectionObserver” in window) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImage.srcset = lazyImage.dataset.srcset;
lazyImage.classList.remove(“lazy”);
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
} else {
// Possibly fall back to a more compatible method here
}
});
边栏推荐
- dsPIC33EP 时钟初始化程序
- “军备竞赛”时期的对比学习
- [paper reading] kgat: knowledge graph attention network for recommendation
- SQL Server monitoring statistics blocking script information
- 分享.NET 轻量级的ORM
- 2022年流动式起重机司机考试题库及模拟考试
- 括号匹配问题(STL)
- 手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
- The first product of Sepp power battery was officially launched
- Glide Mastery
猜你喜欢

赛克瑞浦动力电池首台产品正式下线

Ad20 make logo

2022鹏城杯web
![[dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M

2022年危险化学品经营单位主要负责人特种作业证考试题库及答案

How did automated specification inspection software develop?

爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架

基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平

非技術部門,如何參與 DevOps?
![C语言实现QQ聊天室小项目 [完整源码]](/img/4e/b3703ac864830d55c824e1b56c8f85.png)
C语言实现QQ聊天室小项目 [完整源码]
随机推荐
【tcp】服务器上tcp连接状态json形式输出
WorkManager学习一
微信核酸检测预约小程序系统毕业设计毕设(6)开题答辩PPT
函数///
手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
SqlServer定时备份数据库和定时杀死数据库死锁解决
LSTM应用于MNIST数据集分类(与CNN做对比)
中职组网络安全2021年江苏省省赛题目5套题目环境+解析全有需要的私信我
想请教一下,十大券商有哪些?在线开户是安全么?
AtCoder Beginner Contest 258「ABCDEFG」
数据库中的范式:第一范式,第二范式,第三范式
How do programmers live as they like?
Go-3-第一个Go程序
基于昇腾AI丨爱笔智能推出银行网点数字化解决方案,实现从总部到网点的信息数字化全覆盖
Error: module not found: error: can't resolve 'xxx' in 'XXXX‘
vite//
Window下线程与线程同步总结
分享.NET 轻量级的ORM
非技術部門,如何參與 DevOps?