definition Image delay loading is also called lazy loading. It delays loading of images or loads certain images only when certain conditions are met. It is usually used for web pages with many images. It can reduce the number of requests or delay the number of requests and optimize performance. Presentation form [1] Delayed loading, use setTimeout or setInterval to delay loading. If the user leaves before loading, the loading will naturally not proceed. 【2】Conditional loading: asynchronous loading starts only when certain conditions are met or triggered. 【3】Visible area loading, only load the area that the user can see. This is mainly achieved by monitoring the scroll bar. Generally, loading starts when it is very close to the bottom edge that the user can see. This ensures that the image is connected when the user pulls down, and there will be no long pauses. Basic steps [1] All images on the webpage are set to the same image [2] Add the attribute data-original = "img/test.jpg" to the image to save the real address of the image [3] When certain conditions are triggered, automatically change the src attribute of the image in the area to the real address application 【Click the button to display the image】 - <!DOCTYPE html >
- < html lang = "en" >
- < head >
- < meta charset = "UTF-8" >
- < title > Document </ title >
- < style >
- body{
- margin: 0;
- }
- img{
- height: 100px;
- width: 100px;
- }
- </ style >
- </ head >
- < body >
- < button > Load image </ button >
- < img src = "#" alt = "test" data-original = "img/test.png" >
- < script >
- var oBtn = document .getElementsByTagName('button')[0];
- var oImg = document .images[0];
- oBtn.onclick = function (){
- oImg.src = "img/loading.gif" ;
- if (oImg.dataset) {
- aftLoadImg(oImg,oImg.dataset.original);
- }else{
- aftLoadImg(oImg,oImg.getAttribute("data-original"));
- }
- }
- function aftLoadImg(obj,url){
- var oImg = new Image();
- oImg.onload = function (){
- obj.src = oImg.src ;
- }
- oImg.src = url ;
- }
- </ script >
- </ body >
- </ html >
【Image displayed in the visible area】 - <!DOCTYPE html>
- <html lang= "en" >
- <head>
- <meta charset= "UTF-8" >
- <title>Document</title>
- <style>
- body{
- margin: 0 ;
- }
- ul{
- margin: 0 ;
- padding: 0 ;
- list-style: none;
- }
- img{
- border: none;
- vertical-align: middle;
- }
- .in{
- border: 1px solid black;
- margin: 10px;
- text-align: center;
- height: 400px;
- width: 400px;
- float : left;
- }
- .in img{
- height: 400px;
- width: 400px;
- }
- </style>
- </head>
- <body>
- <ul class = "list" >
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img1.gif" ></li>
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img2.gif" ></li>
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img3.gif" ></li>
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img4.gif" ></li>
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img1.gif" ></li>
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img2.gif" ></li>
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img3.gif" ></li>
- <li class = "in" ><img src= "img/loading.gif" alt= "Test" data-original = "img/img4.gif" ></li>
- </ul>
-
- <script>
- var oBtn = document.getElementsByTagName( 'button' )[ 0 ];
- var aImages = document.images;
- loadImg(aImages);
- window.onscroll = function(){
- loadImg(aImages);
- };
- function loadImg(arr){
- for ( var i = 0 ,len = arr.length; i < len; i++){
- if (arr[i].getBoundingClientRect().top < document.documentElement.clientHeight && !arr[i].isLoad){
- arr[i].isLoad = true ;
- arr[i].style.cssText = "transition: ''; opacity: 0;"
- if (arr[i].dataset){
- aftLoadImg(arr[i],arr[i].dataset.original);
- } else {
- aftLoadImg(arr[i],arr[i].getAttribute( "data-original" ));
- }
- (function(i){
- setTimeout(function(){
- arr[i].style.cssText = "transition: 1s; opacity: 1;"
- }, 16 )
- })(i);
- }
- }
- }
- function aftLoadImg(obj,url){
- var oImg = new Image();
- oImg.onload = function(){
- obj.src = oImg.src;
- }
- oImg.src = url;
- }
-
- </script>
- </body>
- </html>
|