PhotoSwipe JavaScript Image Gallery

5.4.4 · active · verified Sun Apr 19

PhotoSwipe v5.4.4 is a highly customizable JavaScript image gallery and lightbox designed for touch devices. It supports common features like swiping, zooming, and pan gestures, and includes core and lightbox components for flexible integration. The library maintains an active release cadence, frequently publishing patch and minor versions to address bugs, enhance accessibility, and improve browser compatibility. Key differentiators include robust touch support, a modular architecture allowing for dynamic import of its core, extensive customization options via filters and options, and strong focus on accessibility (ARIA attributes, focus trapping) and offline functionality. It ships with TypeScript types, facilitating modern development workflows. It offers a significant rewrite compared to its v4 predecessor, focusing on modern web standards and modularity.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up a basic PhotoSwipe gallery with multiple images using HTML data attributes and initialize the lightbox.

import PhotoSwipeLightbox from 'photoswipe/lightbox';
import 'photoswipe/photoswipe.css';

// 1. Define your gallery items in HTML with data attributes
//    <div id="my-gallery">
//      <a href="full-image-1.jpg" data-pswp-width="1200" data-pswp-height="800" target="_blank">
//        <img src="thumbnail-1.jpg" alt="Image 1">
//      </a>
//      <a href="full-image-2.jpg" data-pswp-width="1000" data-pswp-height="1500" target="_blank">
//        <img src="thumbnail-2.jpg" alt="Image 2">
//      </a>
//    </div>

// 2. Initialize the PhotoSwipe Lightbox
const lightbox = new PhotoSwipeLightbox({
  gallery: '#my-gallery',       // Selector for the gallery container
  children: 'a',                // Selector for individual gallery items
  pswpModule: () => import('photoswipe'), // Dynamically import the core module
  
  // Optional: Set options for the core PhotoSwipe instance
  // easing: 'cubic-bezier(.175, .885, .32, 1.275)',
  // bgOpacity: 0.9,
});

// 3. Initialize the lightbox
lightbox.init();

// To open programmatically:
// lightbox.loadAndOpen(0); // Opens the first item

view raw JSON →