Created
August 8, 2019 01:08
-
-
Save enqtran/b85904229a170ba9ef2c7de4368a7eb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- Let's load this in-viewport image normally --> | |
| <img src="hero.jpg" alt="…"> | |
| <!-- Let's lazy-load the rest of these images --> | |
| <img data-src="unicorn.jpg" alt="…" loading="lazy" class="lazyload"> | |
| <img data-src="cats.jpg" alt="…" loading="lazy" class="lazyload"> | |
| <img data-src="dogs.jpg" alt="…" loading="lazy" class="lazyload"> | |
| <script> | |
| if ('loading' in HTMLImageElement.prototype) { | |
| const images = document.querySelectorAll('img.lazyload'); | |
| images.forEach(img => { | |
| img.src = img.dataset.src; | |
| }); | |
| } else { | |
| // Dynamically import the LazySizes library | |
| const script = document.createElement('script'); | |
| script.src = | |
| 'https://cdnjs.cloudflare.com/ajax/libs/lazysizes/4.1.8/lazysizes.min.js'; | |
| document.body.appendChild(script); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment