当前位置:网站首页>Take you to understand the lazy loading of pictures
Take you to understand the lazy loading of pictures
2022-06-23 22:10:00 【Great inventor】
Lazy loading principle
A picture is a <img> label , Whether the browser initiates a request for a picture is based on <img> Of src attribute , So the key to lazy loading is , When the picture doesn't enter the visual area , Not yet <img> Of src assignment , So the browser won't send the request , Wait until the picture enters the visual area src assignment .
The principle of lazy image loading is that we set the image first data-src attribute ( Of course, it can also be any other , As long as it doesn't send http
Just ask , The function is to access the value ) Value is its picture path , Because it's not src, So it will not be sent http request . Then we calculate the page scrollTop
The sum of the height of and the height of the browser , If the picture is an example, the coordinates at the top of the page
Y( Relative to the whole page , Not the browser window ) Less than the sum of the first two , It shows that the picture is about to be displayed ( The right time , It could be Other situations ), Then we will data-
src Property is replaced with src Attribute is enough
js Code :
// onload Is to bind events after all resource files are loaded
window.onload = function(){// Get a list of pictures , namely img Tag list
var imgs = document.querySelectorAll('img');// Get the distance to the top of the browser
function getTop(e){return e.offsetTop;
}
// Lazy loading implementation
function lazyload(imgs){// Height of visible area
var h = window.innerHeight;
// Scroll area height
var s = document.documentElement.scrollTop || document.body.scrollTop;
for(var i=0;i<imgs.length;i++){// When the distance between the image and the top is greater than the sum of the visible area and the scrolling area, the image will be loaded lazily
if ((h+s)>getTop(imgs[i])) {// The truth is that the page starts with 2 Second blank , So use setTimeout timing 2s
(function(i){ setTimeout(function(){// Execute the function immediately without adding i It will be equal to 9
// Load pictures or other resources invisibly ,
// Create a temporary picture , This picture will not go to the page in memory . Realize invisible loading
var temp = new Image();
temp.src = imgs[i].getAttribute('data-src');// Only once // onload Judge that the picture is loaded , The picture is loaded , Assigned to it again dom node
temp.onload = function(){// Get custom properties data-src, Replace the fake picture with the real picture
imgs[i].src = imgs[i].getAttribute('data-src')}
},2000)
})(i)
}
}
}
lazyload(imgs);
// Scrolling function
window.onscroll =function(){lazyload(imgs);
}
}
边栏推荐
- Assembly deployment process
- To develop AI face comparison, how to output multiple faces with comparative similarity?
- HDLBits-&gt; Circuits-&gt; Arithmetic Circuitd-&gt; 3-bit binary adder
- Environment construction of go language foundation
- Simple code and design concept of "back to top"
- Make it simple. This wave of global topology is quite acceptable!
- Selenium批量查询运动员技术等级
- How to deal with high memory in API gateway how to maintain API gateway
- 【Proteus仿真】LCD1602+DS1307按键设置简易时钟
- Tdsql elite challenge CVM voucher usage guide
猜你喜欢

How to calculate individual income tax? You know what?

Icml2022 | robust task representation for off-line meta reinforcement learning based on contrastive learning

Cloud native practice of meituan cluster scheduling system

Sending network request in wechat applet

CAD图在线Web测量工具代码实现(测量距离、面积、角度等)

Teacher lihongyi from National Taiwan University - grade Descent 2

How to improve the content quality of short video, these four elements must be achieved

Selenium batch query athletes' technical grades

北大、加州伯克利大學等聯合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基於未標記數據的結構化知識的領域自適應文本分類)

Data visualization: summer without watermelon is not summer
随机推荐
How to deal with the situation of repeated streaming and chaotic live broadcast in easydss?
Peking University, University of California Berkeley and others jointly | domain adaptive text classification with structured knowledge from unlabeled data (Domain Adaptive Text Classification Based o
Nanny level anti crawling teaching, JS reverse implementation of font anti crawling
[emergency] log4j has released a new version of 2.17.0. Only by thoroughly understanding the cause of the vulnerability can we respond to changes with the same method
How API gateway extends the importance of gateway extension
What is the use of PMP certification?
实验五 模块、包和库
Interpretation of opentelemetry project
How to provide value for banks through customer value Bi analysis
How to correctly divide data into databases and tables
ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示
2021-12-18: find all letter ectopic words in the string. Given two characters
[vulnerability recurrence]log4j vulnerability rce (cve-2021-44228)
什么是股票线上开户?手机开户安全么?
Bi SQL constraints
How ppt creates a visual chart
Notepad++ installing the jsonview plug-in
Relevant logic of transaction code MICn in SAP mm
Dart series: look at me for security. The security feature in dart is null safety
Tencent cloud database tdsql elite challenge Q & A (real-time update)