Get started with lazysizes CDN

MIT licensed

LazySizes: library for lazy-loading images, enhancing page speed by loading media near viewport entry.

Tags:
  • lazy
  • loader
  • lazyloader
  • lazyload
  • performance
  • responsive
  • image
  • responsive images
  • picture
  • srcset
  • respimg
  • respimage
  • include

Stable version

Copied!

How to start using lazysizes CDN


<!DOCTYPE html>
<html>
<head>
    <title>Get started with lazysizes CDN - cdnhub.io</title>
    <link rel="stylesheet" href="styles.css">
    <script src="https://cdn.cdnhub.io/lazysizes/5.3.2/lazysizes.min.js" async></script>
</head>
<body>
    <img data-src="image1.jpg" class="lazyload" alt="Image 1">
    <img data-src="image2.jpg" class="lazyload" alt="Image 2">
    <img data-src="image3.jpg" class="lazyload" alt="Image 3">
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            if ("IntersectionObserver" in window) {
                let lazyImages = document.querySelectorAll(".lazyload");
                let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
                    entries.forEach(function(entry) {
                        if (entry.isIntersecting) {
                            let lazyImage = entry.target;
                            lazyImage.src = lazyImage.dataset.src;
                            lazyImage.classList.remove("lazyload");
                            lazyImageObserver.unobserve(lazyImage);
                        }
                    });
                });
                lazyImages.forEach(function(lazyImage) {
                    lazyImageObserver.observe(lazyImage);
                });
            }
        });
    </script>
</body>
</html>
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!

All versions