From 33d67ffc4f9d9d0d7ad672997ffc5261770d988b Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 28 Oct 2020 18:01:59 -0300 Subject: so far so good --- static/lazy-loading.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 static/lazy-loading.js (limited to 'static/lazy-loading.js') diff --git a/static/lazy-loading.js b/static/lazy-loading.js new file mode 100644 index 00000000..22f95eb0 --- /dev/null +++ b/static/lazy-loading.js @@ -0,0 +1,25 @@ +// Lazy loading! Roll your own. Woot. + +function loadImage(image) { + image.src = image.dataset.original; +} + +function lazyLoad(elements) { + for (const item of elements) { + if (item.intersectionRatio > 0) { + observer.unobserve(item.target); + loadImage(item.target); + } + } +} + +const observer = new IntersectionObserver(lazyLoad, { + rootMargin: '200px', + threshold: 1.0 +}); + +for (const image of document.querySelectorAll('img.lazy')) { + observer.observe(image); +} + +window.lazyLoadingExecuted = true; -- cgit 1.3.0-6-gf8a5