How to lazyload images in blogger

How to lazyload images in blogger. To lazyload images in Blogger, you will need to use JavaScript to modify the way that images are loaded on your blog. Here is a simple way to do this:

How to lazyload images in blogger 

Go to your Blogger dashboard and click on the "Template" tab.

Click on the "Edit HTML" button to access the HTML code for your blog.

Search for the closing </head> tag in the HTML code.

Just above the closing </head> tag, add the following code:

Copy code
<script>
  function lazyLoad(elements) {
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];
      if (element.getBoundingClientRect().top <= window.innerHeight) {
        var img = element.querySelector("img");
        img.src = img.dataset.src;
        element.classList.remove("lazy");
      }
    }
  }

  var lazyImages = document.querySelectorAll(".lazy");
  window.addEventListener("scroll", function () {
    lazyLoad(lazyImages);
  });
  lazyLoad(lazyImages);
</script>
Save your changes to the template.
Now, to lazyload an image on your blog, you will need to add the lazy class to the img element and specify the source of the image using the data-src attribute. For example:

Copy code
<img class="lazy" data-src="image-url" alt="image description">
When the page is loaded, the image will not be loaded until it comes into view. This can help to improve the performance of your blog by reducing the number of images that are loaded at once.

I hope this helps! Let me know if you have any questions.



Comments

Popular posts from this blog

How to connect blogger to google news

Google cloud Wordpress installation

Blogger vs WordPress who is the best