Get started with lazyloadjs CDN

MIT licensed

LazyLoadJS: Deferentially loads hidden images, enhancing page loading speed.

Tags:
  • lazyload
  • js
  • vvo

Stable version

Copied!

How to start using lazyloadjs CDN


<!DOCTYPE html>
<html>
<head>
    <title>Get started with lazyloadjs CDN - cdnhub.io</title>
    <link rel="stylesheet" href="styles.css">
    <script src="https://cdn.cdnhub.io/lazyloadjs/3.2.2/lazyload.min.js" async></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);
                    lazyImage.dataset.src = lazyImage.getAttribute('data-src');
                    lazyImage.classList.add('lazy');
                });
            }
        });
    </script>
</head>
<body>
    <img src="" data-src="image1.jpg" class="lazy" alt="Image 1" width="300" height="200">
    <img src="" data-src="image2.jpg" class="lazy" alt="Image 2" width="300" height="200">
</body>
</html>
Copied!
Copied!

All versions