From 200fb8848e4966d30570692a6d4a6085377de3cc Mon Sep 17 00:00:00 2001 From: Alan Grainger Date: Mon, 13 Jan 2025 14:49:18 +0800 Subject: [PATCH] #51 lightGallery preload images doesn't work with infinite scrolling --- app/public/web.js | 10 ++++++---- app/views/gallery.ejs | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/public/web.js b/app/public/web.js index 865f3ee..d5a380d 100644 --- a/app/public/web.js +++ b/app/public/web.js @@ -1,8 +1,11 @@ +// How many thumbnails to load per "page" fetched from Immich +const PER_PAGE = 50 + class LGallery { items lightGallery element - index = 0 + index = PER_PAGE /** * Create a lightGallery instance and populate it with the first page of gallery items @@ -56,11 +59,10 @@ class LGallery { * https://www.lightgalleryjs.com/demos/infinite-scrolling/ */ loadMoreItems () { - const numberOfItems = 80 if (this.index < this.items.length) { // Append new thumbnails this.items - .slice(this.index, this.index + numberOfItems) + .slice(this.index, this.index + PER_PAGE) .forEach(item => { if (item.video) { this.element.insertAdjacentHTML('beforeend', ``) } }) - this.index += numberOfItems + this.index += PER_PAGE this.lightGallery.refresh() } } diff --git a/app/views/gallery.ejs b/app/views/gallery.ejs index ee60da7..2c14341 100644 --- a/app/views/gallery.ejs +++ b/app/views/gallery.ejs @@ -24,7 +24,30 @@ <% } %> -
+
+ <% + // Load the first 50 thumbnails, then load more as the user scrolls. See web.js + items.slice(0, 50).forEach(item => { + if (item.video) { %> + + data-download-url="<%- item.downloadUrl %>" + <% } %> + > + +
+
+ <% } else { %> + + data-download-url="<%- item.downloadUrl %>" + <% } %> + > + + + <% } + }) %> +