add lazy background image

This commit is contained in:
2026-01-05 13:39:42 +00:00
parent 4b6c093a1c
commit b5a751e237

View File

@@ -1,6 +1,7 @@
export default function LazyLoadInit()
{
document.addEventListener('DOMContentLoaded', LazyLoad());
document.addEventListener('DOMContentLoaded', LazyBG());
}
function LazyLoad() {
@@ -31,3 +32,28 @@ function LazyLoad() {
// Possibly fall back to a more compatible method here
}
}
function LazyBG() {
var lazyImages = [].slice.call(document.querySelectorAll('.lazy-bg'));
if ('IntersectionObserver' in window) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.computedStyleMap.backgroundImage = `url(${lazyImage.dataset.bg})`;
lazyImage.classList.remove('lazy-bg');
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
} else {
// Possibly fall back to a more compatible method here
}
}