Preface
Loading pictures as needed is a very useful feature that not only improves the performance of the website, but also saves you traffic.For friends who use virtual hosts, if your website is a picture website or a website with more pictures, then lazy loading of pictures is a must.
principle
The principle of lazy loading of pictures is also very simple, and the scr attribute of pictures is not assigned by default.When certain conditions are met, the picture address is taken out and assigned to the src attribute.Generally, in order to make the site page have a better user experience, we set a default picture for all pictures in advance, such as one pixel or any other picture (as long as you like).
Practice Code
Let's start with this Demo: http://yunkus.com/demo/load-images-on-demand/
The example uses a mentioned effect (CSS 3). If you want to use more effects, you can download the animate.css library and try different effects.You won't go into much of this here, but you can refer to the use of the animate.css library here:
Animate.css using tutorials (a powerful CSS3 animation library).
CSS Style
.lazy-img-box {
margin: 0 auto;
width: 430px;
overflow: hidden
}
.lazy-img-box li {
list-style: none;
float: left;
width: 400px;
height: 200px;
margin-right: 12px;
margin-bottom: 12px;
}
.lazy-img-box li img {
width: 400px;
height: 200px;
display: block;
}
.animated {
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
@-webkit-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fadeIn {
-webkit-animation-name: fadeIn;
animation-name: fadeIn;
}
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Load pictures on demand (pictures are lazy to load)-Cloud Library Network</title>
<meta name="description" content="Loading pictures on demand not only improves site performance, but also saves you a lot of traffic!" />
<meta name="keywords" content="JavaScript Course,Load pictures on demand,Picture Lazy Loading" />
<link rel="stylesheet" href="./index.css">
</head>
<body>
<ul class="lazy-img-box">
<li><img _src="http://yunkus.com/demo/lib/images/demo-1.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-2.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-3.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-4.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-5.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-6.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-7.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-8.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-9.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-1.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-2.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-3.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-4.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-5.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-6.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-7.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-8.jpg" src="./images/loading.gif" alt=""></li>
<li><img _src="http://yunkus.com/demo/lib/images/demo-9.jpg" src="./images/loading.gif" alt=""></li>
</ul>
<script src="./index.js"></script>
</body>
</html>
JavaScript Code
window.onload = function () {
var lazyImg = document.getElementsByTagName("img");
var lazyImgLen = lazyImg.length;
var lazyImgArray = [];
var winowBroswerHeight = document.documentElement.clientHeight;
// Initial First Screen Picture
loadImg();
// Execute method of loading pictures while scrolling
window.onscroll = loadImg;
// Load pictures on demand
function loadImg() {
for (var i = 0; i < lazyImgLen; i++) {
var getTD = getTopDistance(lazyImg[i]);
var getST = getScrollTop();
if (!lazyImg[i].loaded && getST < getTD && getTD < (getST + winowBroswerHeight)) {
lazyImg[i].src = lazyImg[i].getAttribute("_src");
lazyImg[i].classList.add("animated", "fadeIn");
lazyImg[i].loaded = true; // Mark as loaded
}
}
}
// Gets the distance of the catalog object from the top of the document document
function getTopDistance(obj) {
var TopDistance = 0;
while (obj) {
TopDistance += obj.offsetTop;
obj = obj.offsetParent;
}
return TopDistance;
}
// Get the scroll distance of a scrollbar
function getScrollTop() {
return document.documentElement.scrollTop || document.body.scrollTop;
}
}
No matter where the scrollbar is, as soon as you refresh it, only the pictures that are on the screen will be loaded.In addition, neither the picture above the screen nor the picture below the screen will be loaded as long as it has not entered the screen area.Such a treatment is theoretically superior, but in the application of practice, it may not develop its original role.Imagine that whether you access a page directly from an address or through a search engine, it usually starts at the top of the page. If you are hygienic and want the code to be simpler, the loadImg() method can be changed to the following code:
function loadImg() {
for (var i = 0; i < lazyImgLen; i++) {
var getTD = getTopDistance(lazyImg[i]);
var getST = getScrollTop();
if (!lazyImg[i].loaded && getTD < (getST + winowBroswerHeight)) {
lazyImg[i].src = lazyImg[i].getAttribute("_src");
lazyImg[i].classList.add("animated", "fadeIn");
lazyImg[i].loaded = true; // Mark as loaded
}
}
}
In fact, nothing has changed, but if judgment condition is one less:
getST < getTD
There are exceptions, however, such as when you click a comment from a list page, it is very likely that you skip to the details page and jump directly to the comment area, which is also common when you do not need to change the code to achieve optimal results.
There is also a small problem here. For scroll listening events, write window.onscroll = function(){} as above, only one page can be used, and only one will work if other landowners also use this scroll listening event.Event binding can be used to solve this problem here, and specific methods can be found here: Frequently-used JavaScript Function Encapsulation.