Dynamically add or remove slides
masterYou can modify the slides in a running PhotoSwipe instance by manipulating pswp.options.dataSource.
- If initialized from DOM elements,
dataSourcecontains agalleryelement and anitemsarray of child elements. - If initialized from an array,
dataSourceis the array itself.
Crucial Step: If you modify a slide that is currently active (the current, next, or previous slide), you MUST call pswp.refreshSlideContent(slideIndex) to reload that specific slide with the new data.
// Example: Replacing the current slide's data
pswp.options.dataSource[pswp.currSlide.index] = {
src: 'new-image.jpg',
width: 800,
height: 600
};
pswp.refreshSlideContent(pswp.currSlide.index);
// Example: Adding a new slide to the end
pswp.options.dataSource.push({
src: 'added-image.jpg',
width: 800,
height: 600
});
pswp.refreshSlideContent(pswp.getNumItems() - 1);