Skip to content

Commit

Permalink
✨ Nicer gallery view
Browse files Browse the repository at this point in the history
  • Loading branch information
henrylee97 committed Mar 8, 2024
1 parent 516d675 commit a895eab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
45 changes: 37 additions & 8 deletions assets/js/gallery.js
Original file line number Diff line number Diff line change
@@ -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();
});
15 changes: 12 additions & 3 deletions layouts/shortcodes/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
{{ $name := replace $folder "." "" }}
{{ $name := replace $name "/" "-" }}

<div class="gallery row">
<div class="gallery row" id="{{ $name }}Gallery"></div>
<div class="gallery-holder d-none" data-gallery-target="#{{ $name }}Gallery">
{{ range $idx, $org := $images }}
{{ $image := strings.TrimPrefix "./" $org }}
{{ $image := $.Page.Resources.GetMatch $image }}
Expand All @@ -26,10 +27,18 @@
{{ else }}
{{ $image = $image.Resize "1000x webp" }}
{{ end }}
<div class="item col-sm-6 col-lg-4 px-1 pb-2" data-bs-toggle="modal" data-bs-target="#{{ $name }}Modal" role="button" data-bs-image="{{ $org }}">
{{ $placeholder := $image.Resize "1x1" }}
{{ $placeholder := printf "%dx%d" $image.Width $image.Height | $placeholder.Resize }}
{{ $placeholderColor := index $placeholder.Colors 0 }}
{{ $placeholderColor := replace $placeholderColor "#" "%23" }}
<div class="gallery-item col-sm-6 col-lg-4 px-1 pb-2" data-bs-toggle="modal" data-bs-target="#{{ $name }}Modal" role="button" data-bs-image="{{ $org }}">
<div data-bs-target="#{{ $name }}Carousel" data-bs-slide-to="{{ $idx }}">
<div class="card">
<img src="{{ $image.RelPermalink }}" loading="lazy" class="card-img w-100">
<img
src="data:image/svg+xml;utf8,%3Csvg width='1000' height='{{ $image.Height }}' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='1000' height='{{ $image.Height }}' x='0' y='0' fill='{{ $placeholderColor }}' /%3E%3C/svg%3E"
data-src="{{ $image.RelPermalink }}"
class="lazy card-img w-100"
>
</div>
</div>
</div>
Expand Down

0 comments on commit a895eab

Please sign in to comment.