From a895eab79e6e4bb0cbae592489108067ec6f48a7 Mon Sep 17 00:00:00 2001 From: Henry Lee Date: Fri, 8 Mar 2024 05:01:03 +0000 Subject: [PATCH] :sparkles: Nicer gallery view --- assets/js/gallery.js | 45 +++++++++++++++++++++++++++------ layouts/shortcodes/gallery.html | 15 ++++++++--- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/assets/js/gallery.js b/assets/js/gallery.js index 92034fb..4b1226e 100644 --- a/assets/js/gallery.js +++ b/assets/js/gallery.js @@ -1,11 +1,40 @@ -const galleries = document.querySelectorAll('.gallery'); -galleries.forEach((gallery) => { - const masonry = new Masonry(gallery, { - itemSelector: '.item', - percentPosition: true +// Enable lazy loading for image thumbnails +document.addEventListener("DOMContentLoaded", function () { + const lazyloadImages = document.querySelectorAll(".lazy"); + const imageObserver = new IntersectionObserver(function (entries, observer) { + entries.forEach(function (entry) { + if (entry.isIntersecting) { + const image = entry.target; + image.src = image.dataset.src; + image.classList.remove("lazy"); + imageObserver.unobserve(image); + } + }); }); - imagesLoaded(gallery).on('progress', function () { - masonry.layout(); + lazyloadImages.forEach(function (image) { + imageObserver.observe(image); + }); + +}); + +// Build masonry layout one by one +const galleryHolders = document.querySelectorAll('.gallery-holder'); +galleryHolders.forEach((galleryHolder) => { + const target = document.querySelector(galleryHolder.dataset.galleryTarget); + const masonry = new Masonry(target, { + itemSelector: '.gallery-item', + percentPosition: true, + transitionDuration: 0, }); -}) + + const nItems = galleryHolder.children.length; + for (let i = 0; i < nItems; i++) { + const item = galleryHolder.children[0]; + target.appendChild(item); + masonry.appended([item]); + masonry.layout(); + } + + galleryHolder.remove(); +}); diff --git a/layouts/shortcodes/gallery.html b/layouts/shortcodes/gallery.html index bd4b9ce..4063d3b 100644 --- a/layouts/shortcodes/gallery.html +++ b/layouts/shortcodes/gallery.html @@ -5,7 +5,8 @@ {{ $name := replace $folder "." "" }} {{ $name := replace $name "/" "-" }} -