当前位置:网站首页>The principle and implementation of lazy image loading
The principle and implementation of lazy image loading
2022-06-09 05:20:00 【Kittens love to eat Yu】
Why use lazy image loading ? What is image lazy loading ?
1. principle
Image lazy loading is a way of front-end page optimization , When there are a lot of pictures on the page , Image loading takes a lot of time , Very expensive server performance , It not only affects the rendering speed, but also wastes bandwidth , To solve this problem , Improve user experience , So there is lazy loading to reduce the pressure on the server , Load the content of the visual area first , Other parts are loaded after entering the visual area , To improve performance .
2. Realization
Ideas : When the picture doesn't enter the visual area , Not at first src assignment , So the browser won't send the request , Wait until the picture enters the visual area src assignment . The real address of the picture needs to be stored in data-src in .
The picture doesn't enter the visual area , That is to say, the picture offsetTop The visual height of the page is less than needed , But think about it , When the picture is at the bottom of the page , You need to scroll the page for a distance before you can see the picture , So here we need to meet img.scrollTop < The height of the visible area of the page + The height of the page scrolling , Here is the key to the lazy loading of images , Next, let's look at the implementation of the specific code
- HTML
<img src="loading.gif" data-src="1.jpg" alt="">
<img src="loading.gif" data-src="2.jpg" alt="">
<img src="loading.gif" data-src="3.jpg" alt="">
<img src="loading.gif" data-src="4.jpg" alt="">
<img src="loading.gif" data-src="5.jpg" alt="">
<img src="loading.gif" data-src="6.jpg" alt="">
<img src="loading.gif" data-src="7.jpg" alt="">
<img src="loading.gif" data-src="8.jpg" alt="">
<img src="loading.gif" data-src="9.jpg" alt="">
<img src="loading.gif" data-src="10.jpg" alt="">
......
- CSS
*{
margin: 0;
padding: 0;
}
img{
vertical-align: top;
width: 100%;
height: auto;
}
- JS
let img = document.getElementsByTagName('img');
let n = 0; // Store where pictures are loaded , Avoid traversing from the first picture every time
lazyload();
window.addEventListener('scroll',lazyload);
function lazyload(){
// Monitor page scrolling Events
var seeHeight = window.innerHeight; // Height of visible area
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
for(let i = n; i < img.length; i++){
if(img[i].offsetTop < seeHeight + scrollTop){
if(img[i].getAttribute("src") == 'loading.gif'){
img[i].src = img[i].getAttribute("data-src");
}
n = i + 1;
}
}
}
Image lazy loading can also be used in combination with throttling and anti shake functions , Optimize the page . Next time, let's introduce how to realize throttling and anti shake !
边栏推荐
- Quickly detect high-risk vulnerabilities of common middleware and components in penetration testing
- MarathonLb的负载研究
- Product weekly report issue 28 | CSDN editor upgrade, adding the function of inserting existing videos
- 好榛子出辽阳!
- Linked list
- Thinking about global exception capture - real global exception capture
- Pull down the new project code and make it red
- 宇邦新材深交所上市:市值47亿 第一季扣非净利降18%
- Palindrome linked list leetcode
- MySQL scheduled backup restore
猜你喜欢

wps ppt图片如何一张一张出来

MRNA factory| quantitative detection of LNP encapsulated RNA content by ribogreen
![[django learning notes - 12]: database operation](/img/d8/2c4b6c036532c213477754b6f9758e.png)
[django learning notes - 12]: database operation

ps如何给图像加边框
![[C language] a quick pass operator](/img/b3/df88d5d3945b553c1bb67247eda788.jpg)
[C language] a quick pass operator

宇邦新材深交所上市:市值47亿 第一季扣非净利降18%

故障排查:阿里云轻量应用服务器中的MySQL容器自行停止

MarathonLb的负载研究

Common interview questions

Good hazelnut comes from Liaoyang!
随机推荐
Source code analysis of reentrantreadwritelock of AQS
Previous improvements of CSDN products (up to issue 29)
What did we do from March to April?
Recommend this UI automation testing framework and write use cases as colloquially as possible
Clcnet: Rethink integrated modeling with classified confidence network (with source code download)
2022 "Cyberspace Security" event module B of Jiangxi secondary vocational group - SQL injection test
Good hazelnut comes from Liaoyang!
Ribbon和Feign的对比-带简易例子
Just now, we received a letter of thanks from the Beijing Organizing Committee for the Winter Olympics
好榛子出辽阳!
The half year revenue of mushroom street was 168million yuan: a year-on-year decrease of 29% and an operating loss of 240million yuan
P1743 Audiophobia
Nacos1.1.4 local source code startup
由id获取name调用示例(腾讯IM)
AQS 之 ReentrantReadWriteLock 源码分析
Class related difficulties
爬取html入mysql插入失败
Remove duplicates from sort array -leetcode
拉下新项目代码爆红
validate-npm-package-name