Last active
June 10, 2020 10:52
-
-
Save Blucknote/231831a16080b3aef5b26ad389ff260e to your computer and use it in GitHub Desktop.
self descripted name
This file contains 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
const lazyLoad = function() { | |
let lazyImages = $("img.lazy:visible"); | |
setTimeout(function() { | |
$.each( | |
lazyImages, | |
function() { | |
let lazyImage = $(this)[0]; | |
if ((lazyImage.getBoundingClientRect().top <= window.innerHeight && lazyImage.getBoundingClientRect().bottom >= 0) && getComputedStyle(lazyImage).display !== "none") { | |
lazyImage.src = lazyImage.dataset.src; | |
lazyImage.srcset = lazyImage.dataset.srcset; | |
lazyImage.classList.remove("lazy"); | |
lazyImages = lazyImages.filter(function(image) { | |
return image !== lazyImage; | |
}); | |
} | |
} | |
); | |
}, 200); | |
}; | |
document.addEventListener("DOMContentLoaded", function() { | |
lazyLoad(); | |
document.addEventListener("scroll", lazyLoad); | |
window.addEventListener("resize", lazyLoad); | |
window.addEventListener("orientationchange", lazyLoad); | |
}); | |
<img | |
class="lazy img-fluid" | |
data-src="deffered.jpg" | |
data-srcset="idkdeffered.jpg" | |
src="temporary.jpg" | |
alt="something" | |
title="something" | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment