« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/static/lazy-loading.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/lazy-loading.js')
-rw-r--r--static/lazy-loading.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/static/lazy-loading.js b/static/lazy-loading.js
new file mode 100644
index 0000000..22f95eb
--- /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;