1234567891011121314151617181920212223242526272829303132 |
- // LAZY LODING JS
- // CREDIT: Rahul Nanwani
- // https://css-tricks.com/the-complete-guide-to-lazy-loading-images/
- document.addEventListener("DOMContentLoaded", function() {
- var lazyloadImages = document.querySelectorAll("img.lazy");
- var lazyloadThrottleTimeout;
-
- function lazyload () {
- if(lazyloadThrottleTimeout) {
- clearTimeout(lazyloadThrottleTimeout);
- }
-
- lazyloadThrottleTimeout = setTimeout(function() {
- var scrollTop = window.pageYOffset;
- lazyloadImages.forEach(function(img) {
- if(img.offsetTop < (window.innerHeight + scrollTop)) {
- img.src = img.dataset.src;
- img.classList.remove('lazy');
- }
- });
- if(lazyloadImages.length == 0) {
- document.removeEventListener("scroll", lazyload);
- window.removeEventListener("resize", lazyload);
- window.removeEventListener("orientationChange", lazyload);
- }
- }, 20);
- }
-
- document.addEventListener("scroll", lazyload);
- window.addEventListener("resize", lazyload);
- window.addEventListener("orientationChange", lazyload);
- });
|