Skip to content

Instantly share code, notes, and snippets.

@sergejmueller
Last active May 19, 2018 13:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergejmueller/8234382 to your computer and use it in GitHub Desktop.
Save sergejmueller/8234382 to your computer and use it in GitHub Desktop.
Lazyload-Technik fürs Nachladen der Bilder. Besonderheit: Bilder werden 3 Screens (3 x Browserfentserhöhe) vor der eigentlichen Position geladen, um beim Erreichen des Viewports bereits sichtbar zu sein. Ohne noscript-Fallback.
addEventListener(
'scroll',
function() {
var i,
img,
rect,
images = document.querySelectorAll('[lazyload-src]');
if ( ! images.length ) {
return;
}
for (i = 0; i < images.length; i++) {
img = images[i];
rect = img.getBoundingClientRect();
if ( rect.top >= 0 && rect.top <= window.innerHeight * 3 ) {
img.src = img.getAttribute('lazyload-src');
img.style.visibility = 'visible';
img.removeAttribute('lazyload-src');
}
}
}
);
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" lazyload-src="http://placehold.it/350x150" style="margin-top: 2000px; width:350px; height: 150px; visibility:hidden" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment