当前位置:网站首页>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
}
});
边栏推荐
- How can non-technical departments participate in Devops?
- 一个可以兼容各种数据库事务的使用范例
- 微信核酸检测预约小程序系统毕业设计毕设(7)中期检查报告
- 使用bat命令一键启动常用浏览器
- 5G NR系统架构
- WorkManager学习一
- [observation] with the rise of the "independent station" model of cross-border e-commerce, how to seize the next dividend explosion era?
- 各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
- 《通信软件开发与应用》课程结业报告
- Who is the "conscience" domestic brand?
猜你喜欢

非技术部门,如何参与 DevOps?

微信核酸检测预约小程序系统毕业设计毕设(6)开题答辩PPT

手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起

Blockbuster: the domestic IDE is released, developed by Alibaba, and is completely open source!

How do programmers live as they like?

"Everyday Mathematics" serial 58: February 27

重磅:国产IDE发布,由阿里研发,完全开源!

第五届 Polkadot Hackathon 创业大赛全程回顾,获胜项目揭秘!
![[paper reading] kgat: knowledge graph attention network for recommendation](/img/fa/d2061bc7bd437f062d46a009cf32cf.png)
[paper reading] kgat: knowledge graph attention network for recommendation

【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
随机推荐
谈谈对Flink框架中容错机制及状态的一致性的理解
字符串、、
SLAM 01.人类识别环境&路径的模型建立
沟通的艺术III:看人之间 之倾听
Sqlserver regularly backup database and regularly kill database deadlock solution
NAS and San
【SWT组件】内容滚动组件 ScrolledComposite
Completion report of communication software development and Application
A usage example that can be compatible with various database transactions
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
Review the whole process of the 5th Polkadot Hackathon entrepreneurship competition, and uncover the secrets of the winning projects!
埋点111
Web3 Foundation grant program empowers developers to review four successful projects
C#实现获取DevExpress中GridView表格进行过滤或排序后的数据
想请教一下,十大券商有哪些?在线开户是安全么?
Write double click event
脚手架开发进阶
数组、、、
Blockbuster: the domestic IDE is released, developed by Alibaba, and is completely open source!
2022年危险化学品生产单位安全生产管理人员特种作业证考试题库模拟考试平台操作