frontend: fix lazy loading

Signed-off-by: Nicolas Froger <nicolas@kektus.xyz>
This commit is contained in:
Nicolas Froger 2024-08-24 11:05:33 +02:00
commit 602962b505
No known key found for this signature in database

View file

@ -12,10 +12,14 @@ const postDataTitle = ref(null)
const postDataDescription = ref(null) const postDataDescription = ref(null)
const postActionBtn = ref(null) const postActionBtn = ref(null)
let slidesContainer = ref(null)
let fpCurrentSlide = 0
defineEmits(['scrollableelemententer', 'scrollableelementleave']) defineEmits(['scrollableelemententer', 'scrollableelementleave'])
function onVisible() { function onVisible() {
document.addEventListener('keydown', onFullpageKeyDown) document.addEventListener('keydown', onFullpageKeyDown)
lazyLoadImage(slidesContainer.value.children[0])
} }
function onInvisible() { function onInvisible() {
@ -67,9 +71,6 @@ function postDataToggle() {
} }
} }
let slidesContainer = ref(null)
let fpCurrentSlide = 0
function scrollSlideDir(direction) { function scrollSlideDir(direction) {
scrollSlideToIndex( scrollSlideToIndex(
(((fpCurrentSlide + direction) % slidesContainer.value.children.length) + (((fpCurrentSlide + direction) % slidesContainer.value.children.length) +
@ -101,8 +102,22 @@ function playChildrenVideo(element) {
} }
} }
function lazyLoadImage(element) {
for (const child of element.children) {
if (child.nodeName === 'IMG') {
if (!child.src && child.dataset.src)
child.src = child.dataset.src
return
} else {
lazyLoadImage(child)
}
}
}
function scrollSlideToIndex(index) { function scrollSlideToIndex(index) {
const fromSlide = slidesContainer.value.children[fpCurrentSlide] const fromSlide = slidesContainer.value.children[fpCurrentSlide]
const toSlide = slidesContainer.value.children[index]
lazyLoadImage(toSlide)
stopChildrenVideo(fromSlide) stopChildrenVideo(fromSlide)
slidesContainer.value.scroll({ slidesContainer.value.scroll({
top: 0, top: 0,
@ -110,7 +125,6 @@ function scrollSlideToIndex(index) {
behavior: 'smooth' behavior: 'smooth'
}) })
fpCurrentSlide = index fpCurrentSlide = index
const toSlide = slidesContainer.value.children[index]
playChildrenVideo(toSlide) playChildrenVideo(toSlide)
} }
</script> </script>
@ -167,12 +181,12 @@ function scrollSlideToIndex(index) {
> >
<img <img
class="absolute top-0 left-0 w-full h-dvh object-cover -z-10" class="absolute top-0 left-0 w-full h-dvh object-cover -z-10"
:src="asset.presignedUrl" :data-src="asset.presignedUrl"
v-if="asset.contentType.startsWith('image/')" v-if="asset.contentType.startsWith('image/')"
loading="lazy"
/> />
<video <video
class="absolute top-0 left-0 w-full h-dvh object-cover -z-10" class="absolute top-0 left-0 w-full h-dvh object-cover -z-10"
preload="none"
loop loop
muted="true" muted="true"
playsinline playsinline