43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
/**
|
|
* Последовательно подставляет src из data-src для img внутри [data-case-sequential-images]
|
|
* (порядок — обход DOM). Атрибут data-src снимается после события load/error.
|
|
*/
|
|
export function initCaseSequentialImages() {
|
|
const root = document.querySelector("[data-case-sequential-images]");
|
|
if (!root) return;
|
|
|
|
const list = Array.from(root.querySelectorAll("img[data-src]"));
|
|
if (!list.length) return;
|
|
|
|
let index = 0;
|
|
|
|
function loadNext() {
|
|
if (index >= list.length) return;
|
|
const img = list[index];
|
|
const url = img.getAttribute("data-src");
|
|
if (!url) {
|
|
index += 1;
|
|
queueMicrotask(loadNext);
|
|
return;
|
|
}
|
|
|
|
const onDone = () => {
|
|
img.removeAttribute("data-src");
|
|
index += 1;
|
|
queueMicrotask(loadNext);
|
|
};
|
|
|
|
img.addEventListener("load", onDone, { once: true });
|
|
img.addEventListener("error", onDone, { once: true });
|
|
img.src = url;
|
|
|
|
if (img.complete && img.naturalWidth > 0) {
|
|
img.removeEventListener("load", onDone);
|
|
img.removeEventListener("error", onDone);
|
|
onDone();
|
|
}
|
|
}
|
|
|
|
loadNext();
|
|
}
|