1
0
mirror of https://github.com/Pomax/BezierInfo-2.git synced 2025-09-30 18:19:04 +02:00

improved lazy loading

This commit is contained in:
Pomax
2020-09-13 13:18:15 -07:00
parent a626c2c8cb
commit c0a1207d67
27 changed files with 163 additions and 208 deletions

View File

@@ -0,0 +1,38 @@
/**
* This file takes any <img> with loading="lazy" and rewrites it
* so that it starts to load much earlier than browsers would load
* them, so that they're already done by the time the user gets to
* them. This (mostly) prevents the whole "you can see the image
* loading in", which is super shitty UX.
*/
const images = Array.from(
document.querySelectorAll(`img.LaTeX.SVG[loading=lazy]`)
);
// Deactivate the images
images.forEach((img) => {
img.dataset.src = img.src;
img.src = ``;
img.removeAttribute(`loading`);
});
// Then make their activation conditional on whether
// they're close to being on-screen or not.
let observer = new IntersectionObserver(
function (entries) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
const pos = images.indexOf(img);
images.splice(pos, 1);
}
});
},
{ rootMargin: `100%` }
);
images.forEach((image) => {
observer.observe(image);
});