Index

joshstock.in / master

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
21616 Oct 2024 22:44fb9522elazy loadingJosh Stockin1290G

Blob @ joshstock.in / site / static / js / lazyload.js

application/javascript904 bytesdownload raw
1function lazyload() {
2 const lazy = document.querySelectorAll(".lazy")
3
4 const intersection_handler = (entries, observer) => {
5 entries.forEach(entry => {
6 if (entry.isIntersecting) {
7 const el = entry.target;
8
9 if (el.tagName === "IMG") {
10 el.src = el.src.replace(".jpg", "_full.jpg")
11 }
12 el.style.backgroundImage = el.style.backgroundImage.replace(".jpg", "_full.jpg")
13
14 el.classList.remove('lazy');
15 observer.unobserve(entry.target); // Stop observing once loaded
16 }
17 });
18 };
19
20 const observer = new IntersectionObserver(intersection_handler, {
21 root: null,
22 rootMargin: "0px 0px 50px 0px",
23 threshold: 0.1
24 });
25
26 lazy.forEach(img => observer.observe(img));
27}
28
29document.addEventListener('DOMContentLoaded', lazyload);
30