Pass an array of any items via dataSource option. Its length will determine amount of slides (which may be modified further from numItems event).
Each item should contain data that you need to generate slide (for image slide it would be src (image URL), width (image width), height, srcset, alt).
If these properties are not present in your initial array, you may “pre-parse” each item from itemData filter.
Without Lightbox module
The Lightbox module connects gallery grid DOM to PhotoSwipe, thus when using a button, it’s completely optional and you can just use PhotoSwipe core directly, for example:
To add a custom last slide increase the total number of slides by one using numItems filter and make sure that correct itemData is returned for the last slide.
The filter numItems allows you to override the total number of slides. And itemData will trigger every time PhotoSwipe requests data about the slide, which usually happens before slide is displayed or lazy-loaded.
The example below creates a gallery with 1000 images.
You may completely override default requirements for HTML markup. In the example below we add thumbnail as background-image, a custom attribute that stores image size, and make sure that zoom transition runs from <a> rather than from <img> within.
We also use domItemData filter, instead of itemData. It fires only once per each slide.
import { customHTMLDataSourceTemplate } from ‘@site/src/components/PswpCodePreview/gallery-templates/custom-html-markup-data-source.js’;
If data is provided as array, but you still want to keep zoom animation. This is often the case when using some kind of dynamic image grid with paging or infinite scroll.
How-to:
Pass array of images to dataSource option.
Use thumbEl filter to provide source of thumbnail element. PhotoSwipe will use its coordinates for animation.
Use placeholderSrc filter to provide the source of placeholder image. Alternatively, you may define msrc property in your data array.
Bind click event to gallery elements and call method lightbox.loadAndOpen(3) (where 3 is image index in your array).
In the example below there are ten images in array, but only three are in DOM.