What is photoswipe?
PhotoSwipe is a JavaScript library for creating image galleries that work on both desktop and mobile devices. It provides a responsive, touch-friendly interface for viewing images, with features like zooming, panning, and fullscreen mode.
What are photoswipe's main functionalities?
Basic Gallery Setup
This code sets up a basic PhotoSwipe gallery with two images. It initializes the gallery with default options.
const PhotoSwipe = require('photoswipe');
const PhotoSwipeUI_Default = require('photoswipe/dist/photoswipe-ui-default');
const items = [
{
src: 'path/to/image1.jpg',
w: 600,
h: 400
},
{
src: 'path/to/image2.jpg',
w: 1200,
h: 900
}
];
const options = {};
const gallery = new PhotoSwipe(document.querySelectorAll('.pswp')[0], PhotoSwipeUI_Default, items, options);
gallery.init();
Custom Options
This code demonstrates how to customize the PhotoSwipe gallery with options like starting index, background opacity, and disabling the share element.
const options = {
index: 0, // start at first slide
showHideOpacity: true,
bgOpacity: 0.7,
shareEl: false
};
const gallery = new PhotoSwipe(document.querySelectorAll('.pswp')[0], PhotoSwipeUI_Default, items, options);
gallery.init();
Event Handling
This code shows how to add event listeners to the PhotoSwipe gallery for events like image load completion and gallery closure.
gallery.listen('imageLoadComplete', function(index, item) {
console.log('Image loaded:', item.src);
});
gallery.listen('close', function() {
console.log('Gallery closed');
});
Other packages similar to photoswipe
lightgallery
Lightgallery is a powerful and flexible JavaScript library for creating image and video galleries. It offers a wide range of features including touch and mouse drag support, responsive design, and various plugins for additional functionalities. Compared to PhotoSwipe, Lightgallery provides more built-in features and customization options out of the box.
fancybox
Fancybox is a JavaScript library for displaying images, videos, and other content in a lightbox overlay. It is highly customizable and supports various content types, including HTML and AJAX. Fancybox is known for its ease of use and extensive documentation. While PhotoSwipe focuses on image galleries, Fancybox offers more versatility in terms of content types.
slick-carousel
Slick Carousel is a popular JavaScript library for creating responsive carousels and sliders. It supports various content types, touch and swipe gestures, and multiple customization options. Unlike PhotoSwipe, which is primarily for image galleries, Slick Carousel is more focused on creating sliders and carousels for a variety of content.
PhotoSwipe Repository
JavaScript image gallery for mobile and desktop.
Optionally, install via Bower: bower install photoswipe
.
Location of files
- Generated PhotoSwipe JS, CSS files, SVG & PNG sprites are in the dist/ folder.
- Source files are in the src/ folder.
- Files for the website (demo & documentation) are in the website/ folder.
- The documentation itself is in website/documentation/.
Plugins / extensions / addons
Coded something useful? Email me and I’ll post a link to it here.
Build
To compile PhotoSwipe by yourself, first make sure that you have Node.js, Grunt.js, Ruby and Jekyll installed, then:
-
Clone the repository
git clone https://github.com/dimsemenov/PhotoSwipe.git
-
Go inside the PhotoSwipe folder that you fetched and install Node dependencies
cd PhotoSwipe && npm install
-
Now simply run grunt
to generate the JS and CSS files in the dist
folder and the site in the _site/
folder.
grunt
Optionally:
- Run
grunt watch
to automatically rebuild files when you change files in src/
or in website/
. - Run
grunt nosite
to just build the JS and CSS files (output is folder dist/
). - Run
grunt pswpbuild
to build just JS files. Param --pswp-exclude
allows to exclude modules, for example grunt pswpbuild --pswp-exclude=history
will exclude history module.
Using PhotoSwipe?
If you’ve used PhotoSwipe in some interesting way, or on the site of a popular brand, I’d be very grateful if you shoot me a link to it.
License
Script is licensed under MIT license with one exception: Do not create a public WordPress plugin based on it, as I will develop it. If you need to use it for a public WordPress plugin right now, please ask me by email first. Thanks!
Attribution is not required, but much appreciated, especially if you’re making a product for developers.
About
PhotoSwipe 4.0+ is developed by Dmitry Semenov. But initially script was created in 2011 by Code Computerlove, a digital agency in Manchester, they passed on development in March 2014. You can view source and documentation of old PhotoSwipe (<4.0) in history of this repo.