当前位置:网站首页>Principle of lazy loading of pictures
Principle of lazy loading of pictures
2022-07-04 22:59:00 【Front end sweeping monk】
What is image lazy loading
Similar to large-scale Taobao jd.com and other web pages , There are a lot of product picture information , If we make all the pictures contained in the page load at one time , The loading speed is very slow, and the user experience is very poor .
At present, the popular method is rolling dynamic loading , That is lazy loading , Pictures displayed outside the screen are not loaded by default , As the page scrolls , The picture has entered the display range , Then trigger the loading and display of the picture , It is usually used with the skeleton screen .
The benefits of doing this , First, the page loading speed is fast , It's about saving traffic , Because few users will scroll the page from top to bottom .
The principle of image lazy loading
First the img Labeled src Link to same picture ( The default image ), When js When listening to the picture entering the visual window , Then apply the actual address .
Method 1
The traditional way to do this is , Listen to the scroll After the event , Call the target element ( Green Square ) Of getBoundingClientRect() Method , Get the coordinates that correspond to the upper left corner of the view , Then judge whether it's in the view . The disadvantage of this method is , because scroll Events happen in an intensive way , It takes a lot of calculation , Easy to cause performance problems .
Get the size of the visual window , Judge the cross area
If the picture appears in the viewport , assignment src
Method 2
new IntersectionObserver API, Can be automatically " Observe " Whether elements are visible ,Chrome 51+ Has supported . Because it's visible (visible) The essence is , The target element creates an intersection with the viewport , So this API be called " Cross viewer ".
IntersectionObserver It's the browser's native constructor , Take two parameters :callback Is a callback function when visibility changes ,option It's the configuration object ( This parameter is optional ).
IntersectionObserverEntry Object provides information about the target element , There are six attributes .
time: When visibility changes , It's a high-precision timestamp , The unit is millisecond
target: Observed target elements , It's a DOM Node object
rootBounds: Information about the rectangular area of the root element ,getBoundingClientRect() Return value of method , If there is no root element ( That is, scrolling directly relative to the viewport ), Then return to null
boundingClientRect: Information about the rectangular area of the target element
intersectionRect: Target element and viewport ( Or root element ) The information of the intersection area
intersectionRatio: The visible proportion of the target element , namely intersectionRect Occupy boundingClientRect The proportion of , When fully visible 1, Less than or equal to when completely invisible 0
IntersectionObserver API It's asynchronous , Does not trigger synchronously with the rolling of the target element .
边栏推荐
- Redis入门完整教程:HyperLogLog
- 该如何去选择证券公司,手机上开户安不安全
- Three stage operations in the attack and defense drill of the blue team
- 【机器学习】手写数字识别
- Redis introduction complete tutorial: slow query analysis
- Erik baleog and Olaf, advanced area of misc in the attack and defense world
- 剑指 Offer 65. 不用加减乘除做加法
- 【剑指offer】1-5题
- [the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
- Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
猜你喜欢

SHP data making 3dfiles white film

A complete tutorial for getting started with redis: hyperloglog

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

The overview and definition of clusters can be seen at a glance

The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal

Redis入门完整教程:哈希说明

攻防世界 MISC 进阶区 Ditf

Talk about Middleware

攻防世界 misc 进阶区 2017_Dating_in_Singapore

Redis introduction complete tutorial: slow query analysis
随机推荐
MySQL Architecture - logical architecture
该如何去选择证券公司,手机上开户安不安全
Analog rocker controlled steering gear
[Lua] Int64 support
浅聊一下中间件
The difference between Max and greatest in SQL
Common methods in string class
EditPlus--用法--快捷键/配置/背景色/字体大小
Sword finger offer 68 - I. nearest common ancestor of binary search tree
Logo special training camp section II collocation relationship between words and graphics
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
Breakpoint debugging under vs2019 c release
特征缩放 标准化 归一化
安装人大金仓数据库
【剑指Offer】6-10题
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Redis的持久化机制
Li Kou 98: verify binary search tree
A complete tutorial for getting started with redis: Pipeline