mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-29 17:02:28 +00:00
Add thumbnail fallback to lazy loader
This commit is contained in:
1 parent
ee4ceb95b7
commit
ed6b69b2e0
1 file changed
+41
-3
@@ -94,11 +94,35 @@ class MediaLoader {
|
||||
for (const entry of entries) {
|
||||
if (entry.isIntersecting) {
|
||||
const el = entry.target;
|
||||
const lazySrc = el.dataset.lazy;
|
||||
const lazySrc =
|
||||
typeof el.dataset.lazy === "string"
|
||||
? el.dataset.lazy.trim()
|
||||
: "";
|
||||
|
||||
if (lazySrc) {
|
||||
const fallbackSrc =
|
||||
(typeof el.dataset.fallbackSrc === "string"
|
||||
? el.dataset.fallbackSrc.trim()
|
||||
: "") ||
|
||||
el.getAttribute("data-fallback-src") ||
|
||||
"";
|
||||
|
||||
const handleError = () => {
|
||||
if (!fallbackSrc) {
|
||||
el.removeEventListener("error", handleError);
|
||||
return;
|
||||
}
|
||||
if (el.src !== fallbackSrc) {
|
||||
el.src = fallbackSrc;
|
||||
}
|
||||
el.removeEventListener("error", handleError);
|
||||
};
|
||||
|
||||
el.addEventListener("error", handleError);
|
||||
el.src = lazySrc;
|
||||
delete el.dataset.lazy;
|
||||
}
|
||||
|
||||
delete el.dataset.lazy;
|
||||
// Stop observing once loaded
|
||||
this.observer.unobserve(el);
|
||||
}
|
||||
@@ -109,7 +133,21 @@ class MediaLoader {
|
||||
}
|
||||
|
||||
observe(el) {
|
||||
if (el.dataset.lazy) {
|
||||
if (!el || typeof el.dataset === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!el.dataset.fallbackSrc) {
|
||||
const fallbackAttr =
|
||||
el.getAttribute("data-fallback-src") || el.getAttribute("src") || "";
|
||||
if (fallbackAttr) {
|
||||
el.dataset.fallbackSrc = fallbackAttr;
|
||||
}
|
||||
}
|
||||
|
||||
const lazySrc =
|
||||
typeof el.dataset.lazy === "string" ? el.dataset.lazy.trim() : "";
|
||||
if (lazySrc) {
|
||||
this.observer.observe(el);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user