Last active
May 1, 2022 23:52
-
-
Save aau8/6b9d4401d2001bb9ab5785241cf600da to your computer and use it in GitHub Desktop.
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
// Ленивая загрузка изображений | |
lazyLoading(); | |
function lazyLoading() { | |
const imgElems = document.querySelectorAll("[data-lazy-loading]") | |
const windowHeight = document.documentElement.clientHeight | |
imgShow() | |
window.addEventListener("scroll", function () { | |
imgShow() | |
}) | |
function imgShow() { | |
for (let i = 0; i < imgElems.length; i++) { | |
const img = imgElems[i]; | |
if (img.getBoundingClientRect().top - windowHeight < 500 && (img.getAttribute('src') == '' || img.getAttribute('src') == null)) { | |
img.setAttribute("src", img.dataset.lazyLoading) | |
img.removeAttribute('data-lazy-loading') | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment