How to Lazy load images with JavaScript

Images are one of the most important elements of a website, providing visual appeal and enhancing the user experience.

However, loading too many images at once can lead to slow page load times, which can negatively impact the user experience and SEO.

Lazy loading is a technique that can help solve this issue by loading images only when they become visible on the screen.

This way, the user can see the content faster and the website remains responsive.


What is Lazy Loading?

Lazy loading is a technique where only the required content is loaded, rather than loading all the content at once.

In the case of images, only the images that are visible on the screen are loaded, and the remaining images are loaded as the user scrolls down the page.

This improves page load times and can also save bandwidth, making the website more efficient.

Why Use Lazy Loading?

  1. Improved Page Load Times: Loading only the required images can significantly improve page load times, making the website faster and more responsive.
  2. Reduced Bandwidth Usage: By only loading the required images, the amount of data transferred over the network is reduced, saving bandwidth.
  3. Better User Experience: Faster page load times and a more responsive website result in a better user experience, which can increase user engagement and improve the overall quality of the website.
  4. Improved SEO: Faster page load times and a better user experience can also improve the website’s SEO, as Google considers page load times as a ranking factor.

How to Implement Lazy Loading in JavaScript

There are several ways to implement lazy loading in JavaScript, each with its own advantages and disadvantages.

Here, we will look at the most common approaches.

Intersection Observer API

The Intersection Observer API is a relatively new and highly efficient way to implement lazy loading in JavaScript.

It works by observing when an element comes into view and loading it only when it is required.

Here is a code example that demonstrates how to use the Intersection Observer API to implement lazy loading:

let observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      let image = entry.target;
      image.src = image.dataset.src;
      observer.unobserve(image);
    }
  });
});

let images = document.querySelectorAll("img");
images.forEach(image => {
  observer.observe(image);
});

Scroll Event Listener

The scroll event listener is a simple and straightforward way to implement lazy loading in JavaScript.

It works by listening for the scroll event and loading images only when they become visible on the screen.

Here is a code example that demonstrates how to use the scroll event listener to implement lazy loading:

let images = document.querySelectorAll("img");

function lazyLoad() {
  images.forEach(image => {
    let rect = image.getBoundingClientRect();
    if (rect.top >= 0 && rect.bottom <= window.innerHeight) {
      image.src = image.dataset.src;
    }
  });
}

window.addEventListener("scroll", lazyLoad);
window.addEventListener("resize", lazyLoad);

Conclusion

Lazy loading is a useful technique for improving page load times, reducing bandwidth usage, and enhancing the user experience on a website.

Implementing lazy loading in JavaScript can be done using the Intersection Observer API or the scroll event listener, both of which have their own advantages and disadvantages.

By only loading the required images, websites can be made faster, more responsive, and provide a better user experience, which can ultimately lead to improved SEO.