Convert Figma logo to code with AI

dimsemenov logoPhotoSwipe

JavaScript image gallery for mobile and desktop, modular, framework independent

24,101
3,310
24,101
620

Top Related Projects

:zap: Simple and easy to use lightbox script written in pure JavaScript

jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.

Full featured JavaScript image & video gallery. No dependencies

A touchable jQuery lightbox

JavaScript image viewer.

Quick Overview

PhotoSwipe is a JavaScript image gallery and lightbox library for mobile and desktop devices. It's highly modular, flexible, and optimized for performance, providing a smooth and responsive user experience for viewing and interacting with images on websites.

Pros

  • Responsive and touch-friendly, works well on both mobile and desktop devices
  • Highly customizable with a modular architecture
  • Supports zooming, swiping, and keyboard navigation
  • Optimized for performance with lazy loading and hardware-accelerated transitions

Cons

  • Learning curve can be steep for advanced customizations
  • Documentation could be more comprehensive for some features
  • Requires additional setup for dynamic content loading
  • Some users report occasional issues with specific browser versions

Code Examples

  1. Basic initialization:
import PhotoSwipe from 'photoswipe';
import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default';

const gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
  1. Defining image items:
const items = [
    {
        src: 'https://example.com/image1.jpg',
        w: 1024,
        h: 768
    },
    {
        src: 'https://example.com/image2.jpg',
        w: 1200,
        h: 900
    }
];
  1. Adding custom buttons:
const options = {
    shareButtons: [
        {id:'facebook', label:'Share on Facebook', url:'https://www.facebook.com/sharer/sharer.php?u={{url}}'},
        {id:'twitter', label:'Tweet', url:'https://twitter.com/intent/tweet?text={{text}}&url={{url}}'}
    ]
};

Getting Started

  1. Install PhotoSwipe:

    npm install photoswipe
    
  2. Include PhotoSwipe CSS and JS files in your HTML:

    <link rel="stylesheet" href="path/to/photoswipe.css">
    <link rel="stylesheet" href="path/to/default-skin/default-skin.css">
    <script src="path/to/photoswipe.min.js"></script>
    <script src="path/to/photoswipe-ui-default.min.js"></script>
    
  3. Add PhotoSwipe HTML markup to your page:

    <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
        <!-- Background of PhotoSwipe -->
        <div class="pswp__bg"></div>
        <!-- Slides wrapper with overflow:hidden -->
        <div class="pswp__scroll-wrap">
            <div class="pswp__container">
                <div class="pswp__item"></div>
                <div class="pswp__item"></div>
                <div class="pswp__item"></div>
            </div>
            <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area -->
            <div class="pswp__ui pswp__ui--hidden">
                <!-- ... (UI elements) ... -->
            </div>
        </div>
    </div>
    
  4. Initialize PhotoSwipe in your JavaScript as shown in the code examples above.

Competitor Comparisons

:zap: Simple and easy to use lightbox script written in pure JavaScript

Pros of baguetteBox.js

  • Lightweight and simple to use, with minimal setup required
  • No dependencies, making it easier to integrate into projects
  • Responsive design out of the box, adapting well to different screen sizes

Cons of baguetteBox.js

  • Limited customization options compared to PhotoSwipe
  • Fewer advanced features, such as touch gestures and zoom functionality
  • Less active development and community support

Code Comparison

baguetteBox.js:

baguetteBox.run('.gallery', {
    animation: 'fadeIn',
    noScrollbars: true
});

PhotoSwipe:

var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
    index: 0,
    bgOpacity: 0.85,
    showHideOpacity: true
});
gallery.init();

Both libraries offer straightforward initialization, but PhotoSwipe requires more setup and configuration. baguetteBox.js provides a simpler API with fewer options, while PhotoSwipe offers more granular control over the gallery's behavior and appearance.

In summary, baguetteBox.js is ideal for projects requiring a lightweight, easy-to-implement image gallery with basic functionality. PhotoSwipe is better suited for more complex applications that need advanced features and extensive customization options.

jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.

Pros of Fancybox

  • Easier to set up and use, with more out-of-the-box features
  • Better support for mobile devices and touch gestures
  • More customization options for UI elements and animations

Cons of Fancybox

  • Larger file size and potentially slower performance
  • Less flexible for advanced customization and complex implementations
  • Requires jQuery, which may not be ideal for all projects

Code Comparison

PhotoSwipe initialization:

var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

Fancybox initialization:

$("[data-fancybox]").fancybox({
  // options
});

Both libraries offer powerful image gallery functionality, but they cater to different needs. PhotoSwipe is lightweight and highly customizable, making it ideal for developers who need fine-grained control over their gallery implementation. It excels in performance and is well-suited for large image collections.

Fancybox, on the other hand, provides a more user-friendly experience with its extensive built-in features and easier setup process. It's a great choice for projects that require quick implementation and a polished look without extensive customization.

Ultimately, the choice between PhotoSwipe and Fancybox depends on the specific requirements of your project, such as performance needs, desired features, and development resources available.

Full featured JavaScript image & video gallery. No dependencies

Pros of lightgallery.js

  • More extensive plugin ecosystem, offering additional features like thumbnails, fullscreen, and zoom
  • Better support for touch devices and mobile-friendly gestures
  • Easier customization of UI elements and controls

Cons of lightgallery.js

  • Larger file size and potentially heavier performance impact
  • Less focused on image-specific optimizations compared to PhotoSwipe
  • Steeper learning curve due to more complex API and configuration options

Code Comparison

PhotoSwipe initialization:

var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

lightgallery.js initialization:

lightGallery(document.getElementById('lightgallery'), {
    plugins: [lgZoom, lgThumbnail],
    speed: 500
});

Both libraries offer simple initialization, but lightgallery.js provides more options for plugins and customization directly in the initialization code. PhotoSwipe tends to have a more streamlined approach, focusing primarily on image gallery functionality.

A touchable jQuery lightbox

Pros of Swipebox

  • Lightweight and simple to implement
  • Supports touch gestures for mobile devices
  • Works well with jQuery, making it easy to integrate into existing projects

Cons of Swipebox

  • Less actively maintained compared to PhotoSwipe
  • Fewer customization options and features
  • Limited documentation and community support

Code Comparison

Swipebox initialization:

$(document).ready(function() {
  $('.swipebox').swipebox();
});

PhotoSwipe initialization:

var pswpElement = document.querySelectorAll('.pswp')[0];
var items = [/* array of slide objects */];
var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

Summary

Swipebox is a simpler, more lightweight option that works well with jQuery and provides basic touch gesture support. However, it lacks the extensive features, customization options, and active development of PhotoSwipe. PhotoSwipe offers more advanced functionality and better documentation but requires a bit more setup. The choice between the two depends on project requirements, with Swipebox being suitable for simpler implementations and PhotoSwipe for more complex, feature-rich galleries.

JavaScript image viewer.

Pros of Viewer.js

  • Simpler API and easier to set up for basic image viewing needs
  • Supports a wider range of file types, including PDF and Office documents
  • Smaller file size, leading to faster load times

Cons of Viewer.js

  • Less customizable and fewer advanced features compared to PhotoSwipe
  • Limited mobile gesture support and touch interactions
  • Fewer built-in UI components and controls

Code Comparison

PhotoSwipe initialization:

var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

Viewer.js initialization:

const viewer = new Viewer(document.getElementById('images'), {
  inline: true,
  viewed() {
    viewer.zoomTo(1);
  },
});

Both libraries offer straightforward initialization, but PhotoSwipe requires more setup code for defining items and options. Viewer.js has a simpler API, making it easier to get started quickly.

PhotoSwipe provides more granular control over the gallery's behavior and appearance, while Viewer.js offers a more streamlined approach with fewer configuration options.

Overall, PhotoSwipe is better suited for complex image gallery needs with advanced features, while Viewer.js is ideal for simpler image viewing requirements and broader file type support.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Stand With Ukraine

PhotoSwipe v5 — JavaScript image gallery and lightbox

Demo | Documentation

Sponsor via OpenCollective Follow on Twitter

Repo structure

  • dist/ - main JS and CSS
  • src/ - source JS and CSS.
    • src/js/photoswipe.js - entry for PhotoSwipe Core.
    • src/js/lightbox/lightbox.js - entry for PhotoSwipe Lightbox.
  • docs/ - documentation markdown files.
  • demo-docs-website/ - website with documentation, demos and manual tests.
  • build/ - rollup build config.

To build JS and CSS in dist/ directory, run npm run build.

To run the demo website and automatically rebuild files during development, run npm install in demo-docs-website/ and npm run watch in the root directory.

Older versions

Documentation for the old version (v4) can be found here and the code for 4.1.3 is here.


This project is tested with BrowserStack.

NPM DownloadsLast 30 Days