Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aau8/6b9d4401d2001bb9ab5785241cf600da to your computer and use it in GitHub Desktop.
Save aau8/6b9d4401d2001bb9ab5785241cf600da to your computer and use it in GitHub Desktop.
// Ленивая загрузка изображений
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