|
| 1 | +import PhotoSwipe from 'photoswipe' |
| 2 | +import PhotoSwipeLightbox from 'photoswipe/lightbox' |
| 3 | +import 'photoswipe/style.css' |
| 4 | + |
| 5 | +const lightboxes: PhotoSwipeLightbox[] = [] |
| 6 | + |
| 7 | +function initPhotoSwipe() { |
| 8 | + lightboxes.forEach((lb) => lb.destroy()) |
| 9 | + lightboxes.length = 0 |
| 10 | + |
| 11 | + const galleries = document.querySelectorAll('[data-pswp-gallery]') |
| 12 | + |
| 13 | + if (galleries.length === 0) return |
| 14 | + |
| 15 | + galleries.forEach((gallery) => { |
| 16 | + const galleryId = gallery.getAttribute('data-pswp-gallery') |
| 17 | + |
| 18 | + const galleryLightbox = new PhotoSwipeLightbox({ |
| 19 | + pswpModule: PhotoSwipe, |
| 20 | + children: 'a', |
| 21 | + gallery: `[data-pswp-gallery="${galleryId}"]`, |
| 22 | + showHideAnimationType: 'fade', |
| 23 | + showAnimationDuration: 333, |
| 24 | + hideAnimationDuration: 333, |
| 25 | + bgClickAction: 'close', |
| 26 | + tapAction: 'close', |
| 27 | + arrowPrev: true, |
| 28 | + arrowNext: true, |
| 29 | + padding: { |
| 30 | + top: 46, |
| 31 | + bottom: 46, |
| 32 | + left: 17, |
| 33 | + right: 17, |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + galleryLightbox.init() |
| 38 | + lightboxes.push(galleryLightbox) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +document.addEventListener('DOMContentLoaded', initPhotoSwipe) |
| 43 | +document.addEventListener('astro:page-load', initPhotoSwipe) |
| 44 | + |
| 45 | +document.addEventListener('astro:before-preparation', () => { |
| 46 | + lightboxes.forEach((lb) => lb.destroy()) |
| 47 | + lightboxes.length = 0 |
| 48 | +}) |
0 commit comments