Fix: enable seamless next page loading during lightGallery sliding, add intelligent loop
This commit is contained in:
+14
-4
@@ -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,9 +70,9 @@ 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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const lgallery = new LGallery()
|
const lgallery = new LGallery()
|
||||||
Reference in New Issue
Block a user