How we can create a lazy load function for images using JavaScript? Solution: See this Lazy Load Images on Scroll Using JavaScript, Lazy Loading Gallery.
Previously I have shared many image related programs, this is lazy loading feature for show elements fast and images later. Basically, Lazy loading is an optimization technique for online content, be it a website or a web app. It loads only the required section and delays the remaining. Like we have 10 images, it will load only those images which are on display area and left others will load on scrolling.
Today you will learn to create JavaScript Lazy Loading Gallery. Basically, there are 20 random images with the same width and height and placed in a column. Each row has only 2 images, which means the column has 10-row sections. First, it will load only the images which are on display right now, then when you scroll down then the others will load.
So, Today I am sharing Lazy Load Images on Scroll Using JavaScript. There I have used HTML and CSS to create the layout design and JavaScript for the lazy loading functions. There I have not used jQuery or any other JS library, you can call it a pure JS program. You can use this program on your website for fast loading. If you use any CMS then use plugins that are available in the market.
If you are thinking now how this lazy load program actually is, then see the preview given below.
Preview of Lazy Loading Gallery
See this video preview to getting an idea of how this gallery and load feature looks like.
Now you can see this program visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
Lazy Load Images on Scroll Using JavaScript Source Code
Before sharing source code, let’s talk about it. First I have created the main div with class name gallery and placed all the images with a div inside it. Each group has a similar class name and I have used random images from a website. Also, in the HTML file, I have linked other files like CSS and JavaScript.
Now using CSS I have placed all the images and heading in the right place, as you can see in the preview. There I have used flex display command to create the layout and used max-width to creating responsive elements. I gave a 1-second delay for image visible, and gave width, height and other values to the elements.
JavaScript handling here the lazy load feature on scroll in this program. I used JS document.querySelectorAll command to fetch and used if{} else{} statement to declare the conditions and commands. There I have created a function to load images on scroll. Left all other things you will understand after getting the codes, I can’t explain all in writing.
For creating this program you have to create 3 files. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Lazy Loading Images | Webdevtrick.com</title> <link href="https://fonts.googleapis.com/css?family=Squada+One&display=swap" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Lazy Loading Images</h1> <div class="gallery"> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=20" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=19" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=1" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=2" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=3" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=4" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=5" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=6" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=7" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=8" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=9" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=10" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=11" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=12" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=13" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=14" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=15" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=16" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=17" width="400" height="500" alt="Image 1"/> </div> <div class="gallery-item"> <img data-src="https://source.unsplash.com/collection/3694365/800x600/?sig=18" width="400" height="500" alt="Image 1"/> </div> </div> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ * { box-sizing: border-box; } h1 { font-family: 'Squada One', cursive; text-align: center; font-weight: 100; color: #666; font-size: 52px; margin: 100px 0 0; } .gallery { display: flex; flex-wrap: wrap; max-width: 960px; margin: 100px auto; box-shadow: 0 0 50px #333; } .gallery-item { width: 50%; } .gallery-item > img { width: 100%; height: auto; object-fit: cover; display: block; } img { opacity: 1; transition: opacity 1s; } img[data-src] { opacity: 0; } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Code By Webdevtrick ( https://webdevtrick.com ) (function() { var elements = document.querySelectorAll('img[data-src]'); var index = 0; var lazyLoad = function() { if(index >= elements.length) return; var item = elements[index]; if((this.scrollY + this.innerHeight) > item.offsetTop) { var src = item.getAttribute("data-src"); item.src = src; item.addEventListener('load', function() { item.removeAttribute('data-src'); }); index++; lazyLoad(); } }; var init = function() { window.addEventListener('scroll', lazyLoad); lazyLoad(); }; return init(); })(); |
That’s It. Now you have successfully created Lazy Load Images on Scroll Using JavaScript, Lazy Loading Gallery. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
How can I add image titles and description to lazy loading images?
Thanks for any help
nice sir