feat: move scripts to script folder
This commit is contained in:
38
src/scripts/photoswipe.ts
Normal file
38
src/scripts/photoswipe.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import PhotoSwipe from "photoswipe";
|
||||
|
||||
let lightbox: PhotoSwipeLightbox | null = null;
|
||||
|
||||
function initPhotoSwipe() {
|
||||
const links = document.querySelectorAll<HTMLAnchorElement>('a.pswp-link');
|
||||
|
||||
links.forEach((link) => {
|
||||
const img = link.querySelector('img');
|
||||
if (img) {
|
||||
const applyDimensions = () => {
|
||||
link.dataset.pswpWidth = (img.naturalWidth || 1920).toString();
|
||||
link.dataset.pswpHeight = (img.naturalHeight || 1080).toString();
|
||||
};
|
||||
if (img.complete) {
|
||||
applyDimensions();
|
||||
} else {
|
||||
img.addEventListener('load', applyDimensions);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (lightbox) {
|
||||
lightbox.destroy();
|
||||
}
|
||||
|
||||
lightbox = new PhotoSwipeLightbox({
|
||||
gallery: '.prose',
|
||||
children: 'a.pswp-link',
|
||||
pswpModule: PhotoSwipe,
|
||||
allowPanToNext: true,
|
||||
});
|
||||
|
||||
lightbox.init();
|
||||
}
|
||||
|
||||
export { initPhotoSwipe };
|
||||
Reference in New Issue
Block a user