当前位置:网站首页>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
}
});
边栏推荐
- [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年危险化学品生产单位安全生产管理人员特种作业证考试题库模拟考试平台操作
- 基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
- 5G NR系统架构
- Web3 Foundation grant program empowers developers to review four successful projects
- 小程序框架Taro
- [paper reading] kgat: knowledge graph attention network for recommendation
- In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
- Workmanager learning 1
- 一个可以兼容各种数据库事务的使用范例
猜你喜欢
2022年T电梯修理操作证考试题及答案
在C# 中实现上升沿,并模仿PLC环境验证 If 语句使用上升沿和不使用上升沿的不同
Learning notes 5 - high precision map solution
基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
Based on shengteng AI Aibi intelligence, we launched a digital solution for bank outlets to achieve full digital coverage of information from headquarters to outlets
Go-3-第一个Go程序
"Everyday Mathematics" serial 58: February 27
Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
2022鹏城杯web
How did automated specification inspection software develop?
随机推荐
Blockbuster: the domestic IDE is released, developed by Alibaba, and is completely open source!
flink cdc不能监听mysql日志,大家遇到过这个问题吧?
web安全
非技術部門,如何參與 DevOps?
变量///
AD20 制作 Logo
How does redis implement multiple zones?
Secteur non technique, comment participer à devops?
Learning notes 5 - high precision map solution
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
Pseudo class elements -- before and after
微信核酸检测预约小程序系统毕业设计毕设(6)开题答辩PPT
Qt实现json解析
运算符、、
Nuxt//
Singleton mode encapsulates activity management class
Sqlserver regularly backup database and regularly kill database deadlock solution
Learning II of workmanager
Comparative learning in the period of "arms race"
想请教一下,十大券商有哪些?在线开户是安全么?