feat(gallery.ts): change image source to a data attribute and dynamically load it when the image is visible

This commit is contained in:
Sped0n
2023-10-30 17:47:06 +08:00
parent b5aae19c70
commit 071f53071e
3 changed files with 26 additions and 7 deletions

View File

@@ -22,6 +22,21 @@ export function getRandom(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
export function onVisible(
element: HTMLImageElement,
callback: (arg0: HTMLImageElement) => void
) {
new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.intersectionRatio > 0) {
callback(element)
observer.disconnect()
}
})
}).observe(element)
if (!callback) return new Promise((r) => (callback = r))
}
/**
* custom types
*/