当前位置:网站首页>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 .
边栏推荐
- Advanced area of attack and defense world misc 3-11
- Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
- Redis入门完整教程:GEO
- Redis入门完整教程:Redis Shell
- How to choose a securities company? Is it safe to open an account on your mobile phone
- 剑指 Offer 65. 不用加减乘除做加法
- 华泰证券是国家认可的券商吗?开户安不安全?
- 环境加密技术解析
- Redis入門完整教程:Pipeline
- Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
猜你喜欢

MYSQL架构——逻辑架构

Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
How to send a reliable request before closing the page

The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展

MySQL Architecture - logical architecture

堆排序代码详解

Redis: redis configuration file related configuration and redis persistence

Tla+ introductory tutorial (1): introduction to formal methods

Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集

Redis入门完整教程:有序集合详解
随机推荐
Notepad++ -- editing skills
Redis: redis configuration file related configuration and redis persistence
【图论】拓扑排序
华泰证券是国家认可的券商吗?开户安不安全?
Duplicate ADMAS part name
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
Redis getting started complete tutorial: Geo
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
On-off and on-off of quality system construction
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
Advanced area of attack and defense world misc 3-11
Gnawing down the big bone - sorting (II)
Redis入门完整教程:键管理
集群的概述与定义,一看就会
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Business is too busy. Is there really no reason to have time for automation?
Sword finger offer 67 Convert a string to an integer
攻防世界 MISC 进阶区 hong