Get started with vanilla-lazyload CDN

MIT licensed

Vanilla-lazyload is a lightweight library for lazy loading images without using external dependencies.

Tags:
  • lazyload
  • vanilla
  • responsive
  • images
  • picture
  • srcset
  • SEO
  • intersectionObserver
  • sizes
  • progressive
  • performance
  • perfmatters
  • async
  • no-jquery

Stable version

Copied!

How to start using vanilla-lazyload CDN


<!DOCTYPE html>
<html>
<head>
  <title>Get started with vanilla-lazyload CDN - cdnhub.io</title>
  <link rel="stylesheet" href="styles.css">
  <script src="https://cdn.cdnhub.io/vanilla-lazyload/17.8.8/lazyload.min.js" defer></script>
  <script>
    document.addEventListener('DOMContentLoaded', function() {
      const lazyImages = document.querySelectorAll('img.lazy');
      if ('IntersectionObserver' in window) {
        const lazyImageObserver = new IntersectionObserver(function(entries, observer) {
          entries.forEach(function(entry) {
            if (entry.isIntersecting) {
              const lazyImage = entry.target;
              lazyImage.src = lazyImage.dataset.src;
              lazyImage.classList.remove('lazy');
              lazyImageObserver.unobserve(lazyImage);
            }
          });
        });

        lazyImages.forEach(function(lazyImage) {
          lazyImageObserver.observe(lazyImage);
        });
      }
    });
  </script>
</head>
<body>
  <header>
    <h1>Lazy Loading Example</h1>
  </header>
  <main>
    <p>Scroll down to see the lazy loaded images.</p>
    <img src="placeholder.jpg" class="lazy" data-src="image1.jpg" alt="Image 1" width="300" height="200">
    <img src="placeholder.jpg" class="lazy" data-src="image2.jpg" alt="Image 2" width="300" height="200">
    <img src="placeholder.jpg" class="lazy" data-src="image3.jpg" alt="Image 3" width="300" height="200">
  </main>
</body>
</html>
Copied!
Copied!
Copied!
Copied!

All versions