docs/options.md
Number, 0.8.
Background backdrop opacity, always define it via this option and not via CSS rgba color.
<PswpCodePreview galleryID="test-bgopacity">import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-bgopacity',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
bgOpacity: 0.2,
});
lightbox.init();
Number, 0.1. Spacing between slides. Defined as ratio relative to the viewport width (0.1 = 10% of viewport).
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-spacing',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
spacing: 0.5, // 50% of viewport width
});
lightbox.init();
Boolean, true. Allow swipe navigation to the next slide when the current slide is zoomed. Does not apply to mouse events.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-spacing',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
allowPanToNext: false
});
lightbox.init();
Boolean, true. If set to true you'll be able to swipe from the last to the first image. Option is always false when there are less than 3 slides.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-loop',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
loop: false
});
lightbox.init();
Boolean, undefined. By default PhotoSwipe zooms image with ctrl-wheel, if you enable this option - image will zoom just via wheel.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-wheelToZoom',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
wheelToZoom: true
});
lightbox.init();
Boolean, true. Pinch touch gesture to close the gallery.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-pinch-to-close',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
pinchToClose: false
});
lightbox.init();
Boolean, true. Vertical drag gesture to close the PhotoSwipe.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--close-on-vertical-drag',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
closeOnVerticalDrag: false
});
lightbox.init();
Object, { top: 0, bottom: 0, left: 0, right: 0 }. Slide area padding (in pixels).
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-padding',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
padding: { top: 20, bottom: 40, left: 100, right: 100 }
});
lightbox.init();
Function, should return padding object. The option is checked frequently, so make sure it's performant. Overrides padding option if defined. For example:
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-paddingfn',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
paddingFn: (viewportSize, itemData, index) => {
return {
// check based on slide index
top: index === 0 ? 100 : 0,
// check based on viewport size
bottom: viewportSize.x < 600 ? 0 : 200,
// check based on image size
left: itemData.w < 2000 ? 50 : 0,
right: 0
};
}
});
lightbox.init();
Number, 333. Transition duration in milliseconds, can be 0. Example here.
String, 'cubic-bezier(.4,0,.22,1)'. CSS easing function for open/close/zoom transitions. Example here.
Boolean, true. Esc key to close.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-esc-key',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
escKey: false
});
lightbox.init();
Boolean, true. Left/right arrow keys for navigation.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-arrow-keys',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
arrowKeys: false
});
lightbox.init();
Boolean, true. Trap focus within PhotoSwipe element while it's open.
Boolean, true. Restore focus the last active element after PhotoSwipe is closed.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-restore-focus',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
returnFocus: false
});
lightbox.init();
Boolean, true. If image is not zoomable (for example, smaller than viewport) it can be closed by clicking on it.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--clickToCloseNonZoomable',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
clickToCloseNonZoomable: false
});
lightbox.init();
<div class="pswp-gallery" id="gallery--clickToCloseNonZoomable">
<a data-pswp-width="300" data-pswp-height="200" href="https://dummyimage.com/300x200/555/fff/?text=1st-300x200" target="_blank">
</a>
<a data-pswp-width="1000" data-pswp-height="1000" href="https://dummyimage.com/1000x1000/555/fff/?text=2nd-1000x1000" target="_blank">
</a>
<a data-pswp-width="100" data-pswp-height="700" href="https://dummyimage.com/100x700/555/fff/?text=3rd-100x700" target="_blank">
</a>
</div>
Refer to click and tap actions page.
Number (ms), 2000. Delay before the loading indicator will be displayed, if image is loaded during it - the indicator will not be displayed at all. Can be zero.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-preloader-delay',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
preloaderDelay: 0
});
lightbox.init();
String, /. Used for slide count indicator ("1 of 10 ").
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-indexIndicatorSep',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
indexIndicatorSep: ' of '
});
lightbox.init();
Function {x: width, y: height}, undefined. A function that should return slide viewport width and height, in format {x: 100, y: 100}.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-getViewportSizeFn',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
getViewportSizeFn: function(options, pswp) {
return {
x: document.documentElement.clientWidth - 200,
y: window.innerHeight
};
}
});
lightbox.init();
Refer to Adjusting zoom level page for more info. The default values are described there too.
String, 'The image cannot be loaded'.
Message to display when the image wasn't able to load. If you need to display HTML - use contentErrorElement filter.
<PswpCodePreview>import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--errorMsg',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
errorMsg: 'The photo cannot be loaded'
});
lightbox.init();
<div class="pswp-gallery pswp-gallery--single-column" id="gallery--errorMsg">
<a data-pswp-width="1200" data-pswp-height="800" href="https://picsum.photos/1200/800" target="_blank">
</a>
<a data-pswp-width="800" data-pswp-height="600" href="https://example.com/broken-image-link" target="_blank">
</a>
<a data-pswp-width="1000" data-pswp-height="1000" href="https://dummyimage.com/1000x1000/555/fff/?text=2nd-1000x1000" target="_blank">
</a>
<a data-pswp-width="800" data-pswp-height="600" href="https://example.com/another-broken-image-link" target="_blank">
</a>
</div>
Array, [1, 2]. Lazy loading of nearby slides based on direction of movement. Should be an array with two integers, first one - number of items to preload before the current image, second one - after the current image. Two nearby images are always loaded.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-preload',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
preload: [1, 4]
});
lightbox.init();
String, undefined. Class that will be added to the root element of PhotoSwipe, may contain multiple separated by space. Example on Styling page.
DOMElement, document.body. Element to which PhotoSwipe dialog will be appended when it opens.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-appendToEl',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
appendToEl: document.querySelector('#__docusaurus')
});
lightbox.init();
Integer, 4000. Maximum width of image to animate, if initial rendered image width is larger than this value - the opening/closing transition will be automatically disabled.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-maxWidthToAnimate',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
maxWidthToAnimate: 800,
});
lightbox.init();
See getting started and data sources for examples.
Element, NodeList, or CSS selector (string) for the gallery element.
Element, NodeList, or CSS selector (string) for elements within gallery. For example, link elements. If not defined or set to false - root gallery node will be used.
Function or Module, undefined. A function that should return import() promise (if you need dynamic import), for example:
pswpModule: () => import('photoswipe/dist/photoswipe.esm.js');
Or the PhotoSwipe Core module itself, for example:
import PhotoSwipeLightbox from 'photoswipe/dist/photoswipe-lightbox.esm.js';
import PhotoSwipe from 'photoswipe/dist/photoswipe.esm.js';
const lightbox = new PhotoSwipeLightbox({
pswpModule: PhotoSwipe
// ...
});
Boolean, true. Loads the first slide image in parallel with PhotoSwipe Core (while PhotoSwipe is opening).
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-appendToEl',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
preloadFirstSlide: false
});
lightbox.init();
A list of options that might need a translation:
closeTitle: 'Close',
zoomTitle: 'Zoom',
arrowPrevTitle: 'Previous',
arrowNextTitle: 'Next',
errorMsg: 'The image cannot be loaded',
indexIndicatorSep: ' / ',
Example:
<PswpCodePreview galleryID="test-i18n">import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--test-i18n',
children: 'a',
closeTitle: 'Close the dialog',
zoomTitle: 'Zoom the photo',
arrowPrevTitle: 'Go to the previous photo',
arrowNextTitle: 'Go to the next photo',
errorMsg: 'The photo cannot be loaded',
indexIndicatorSep: ' of ',
pswpModule: () => import('/photoswipe/photoswipe.esm.js'),
preloadFirstSlide: false
});
lightbox.init();