From b5a751e2372cc27985d389280af829a04f02970f Mon Sep 17 00:00:00 2001 From: Steve Ross Date: Mon, 5 Jan 2026 13:39:42 +0000 Subject: [PATCH] add lazy background image --- resources/js/lib/Lazy.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/resources/js/lib/Lazy.js b/resources/js/lib/Lazy.js index 89249d5..72404d0 100644 --- a/resources/js/lib/Lazy.js +++ b/resources/js/lib/Lazy.js @@ -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 + } +}