当前位置:网站首页>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
}
});
边栏推荐
- Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
- dsPIC33EP 时钟初始化程序
- Web3基金会「Grant计划」赋能开发者,盘点四大成功项目
- TSQL–标示列、GUID 、序列
- Solution of ellipsis when pytorch outputs tensor (output tensor completely)
- 谈谈对Flink框架中容错机制及状态的一致性的理解
- 数据库中的范式:第一范式,第二范式,第三范式
- 赛克瑞浦动力电池首台产品正式下线
- Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
- WorkManager的学习二
猜你喜欢
Go-3-第一个Go程序
Learning II of workmanager
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
[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
风控模型启用前的最后一道工序,80%的童鞋在这都踩坑
DGL中异构图的一些理解以及异构图卷积HeteroGraphConv的用法
基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
非技術部門,如何參與 DevOps?
Go语言-1-开发环境配置
How do programmers live as they like?
随机推荐
使用GBase 8c数据库过程中报错:80000502,Cluster:%s is busy,是怎么回事?
Activity enter exit animation
Pseudo class elements -- before and after
【tcp】服务器上tcp连接状态json形式输出
沟通的艺术III:看人之间 之倾听
上拉加载原理
Learning notes 5 - high precision map solution
Go project practice - Gorm format time field
C language QQ chat room small project [complete source code]
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
基于昇腾AI丨爱笔智能推出银行网点数字化解决方案,实现从总部到网点的信息数字化全覆盖
Web3 Foundation grant program empowers developers to review four successful projects
字符串、、
一个可以兼容各种数据库事务的使用范例
2022鹏城杯web
Shortcut keys for vscode
第五届 Polkadot Hackathon 创业大赛全程回顾,获胜项目揭秘!
图片懒加载的方案
小程序框架Taro
脚手架开发基础