Fix: enable seamless next page loading during lightGallery sliding, add intelligent loop

This commit is contained in:
John B
2026-05-16 22:52:30 +03:00
parent 2b903f031e
commit a14e168190
+13 -3
View File
@@ -16,6 +16,7 @@ class LGallery {
this.lightGallery = lightGallery(this.element, Object.assign({ this.lightGallery = lightGallery(this.element, Object.assign({
plugins: [lgZoom, lgThumbnail, lgVideo, lgFullscreen, lgHash], plugins: [lgZoom, lgThumbnail, lgVideo, lgFullscreen, lgHash],
speed: 500, speed: 500,
loop: params.items.length <= PER_PAGE,
/* /*
This license key was graciously provided by LightGallery under their This license key was graciously provided by LightGallery under their
GPLv3 open-source project license: GPLv3 open-source project license:
@@ -34,14 +35,23 @@ class LGallery {
this.items = params.items this.items = params.items
const spinner = document.getElementById('loading-spinner') const spinner = document.getElementById('loading-spinner')
let observer = null
if (spinner) { if (spinner) {
const observer = new IntersectionObserver((entries) => { observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) { if (entries[0].isIntersecting) {
lgallery.loadMoreItems(observer, spinner) lgallery.loadMoreItems(observer, spinner)
} }
}, { rootMargin: '200px' }) }, { rootMargin: '200px' })
observer.observe(spinner) observer.observe(spinner)
} }
const preloadThreshold = 8
this.element.addEventListener('lgAfterSlide', (event) => {
if (event.detail.index >= this.index - preloadThreshold) {
lgallery.loadMoreItems(observer, spinner)
}
})
} }
/** /**
@@ -60,8 +70,8 @@ class LGallery {
this.lightGallery.refresh() this.lightGallery.refresh()
} else { } else {
// Remove the loading spinner and stop observing once all items are loaded // Remove the loading spinner and stop observing once all items are loaded
observer.disconnect() if (observer) observer.disconnect()
spinner.remove() if (spinner) spinner.remove()
} }
} }
} }