Parvus

An accessible, open-source image lightbox with no dependencies.

README

Parvus


Overlays are not recommended to use, but if you need one, you can consider using Parvus. Parvus is an open-source image lightbox that aims to be accessible and has no dependencies.

Screenshot of Parvus. It shows the first picture of a gallery.


Installation


Download


- CSS:
  - dist/css/parvus.min.css (minified) or
  - dist/css/parvus.css (un-minified)
- JavaScript:
  - dist/js/parvus.min.js (minified) or
  - dist/js/parvus.js (un-minified)

Link the .css and .js files to your HTML file. Your HTML code should look like this:

  1. ```html
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.   <meta charset="UTF-8">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">
  7.   <title>Page title</title>
  8.   !-- CSS -->
  9.   <link href="path/to/parvus.min.css" rel="stylesheet">
  10. </head>
  11. <body>
  12.   !-- HTML content -->
  13.   !-- JS -->
  14.   <script src="path/to/parvus.min.js"></script>
  15. </body>
  16. </html>
  17. ```

Package Managers


You can also install Parvus using npm or yarn, like any other dependency:

  1. ```
  2. npm install parvus
  3. ```

or

  1. ```
  2. yarn add parvus
  3. ```

After installation, you can import Parvus into your JavaScript codebase:

  1. ```js
  2. import Parvus from 'parvus'
  3. ```

Make sure to include the corresponding SCSS or CSS file.

Usage


The standard way to use Parvus is by linking a thumbnail image with the class lightbox to a larger image.

  1. ```html
  2. <a href="path/to/image.jpg" class="lightbox">
  3.   <img src="path/to/thumbnail.jpg" alt="">
  4. </a>
  5. ```

Initialize the script by running:

  1. ```js
  2. const prvs = new Parvus()
  3. ```

Captions


If you want to show a caption under the image, you can add a data-caption attribute.

  1. ```html
  2. <a href="path/to/image.jpg" class="lightbox" data-caption="I'm a caption">
  3.   <img src="path/to/thumbnail.jpg" alt="">
  4. </a>
  5. ```

Alternatively, you can set the option captionsSelector to select the captions from the innerHTML of an element.

  1. ```html
  2. <a href="path/to/image.jpg" class="lightbox">
  3.   <figure class="figure">
  4.     <img src="path/to/thumbnail.jpg" alt="">
  5.     <figcaption class="figure__caption">
  6.       <p>I'm a caption</p>
  7.     </figcaption>
  8.   </figure>
  9. </a>
  10. ```

  1. ```js
  2. const prvs = new Parvus({
  3.   captionsSelector: '.figure__caption',
  4. })
  5. ```

Gallery


If you have a group of related images that you would like to combine into a set, you can add a data-group attribute:

  1. ```html
  2. <a href="path/to/image.jpg" class="lightbox" data-group="Berlin">
  3.   <img src="path/to/thumbnail.jpg" alt="">
  4. </a>
  5. <a href="path/to/image_2.jpg" class="lightbox" data-group="Berlin">
  6.   <img src="path/to/thumbnail_2.jpg" alt="">
  7. </a>
  8. //...
  9. <a href="path/to/image_8.jpg" class="lightbox" data-group="Kassel">
  10.   <img src="path/to/thumbnail_8.jpg" alt="">
  11. </a>
  12. ```

Alternatively, you can set the option gallerySelector to combine all images with a specific class within a selector into a group.

  1. ```html
  2. <div class="gallery">
  3.   <a href="path/to/image.jpg" class="lightbox">
  4.     <img src="path/to/thumbnail.jpg" alt="">
  5.   </a>
  6.   <a href="path/to/image_2.jpg" class="lightbox">
  7.     <img src="path/to/thumbnail_2.jpg" alt="">
  8.   </a>
  9.   // ...
  10. </div>
  11. ```

  1. ```js
  2. const prvs = new Parvus({
  3.   gallerySelector: '.gallery',
  4. })
  5. ```

Responsive Images


You can specify different image sources and sizes using the data-srcset and data-sizes attribute.

  1. ```html
  2. <a href="path/to/image.jpg" class="lightbox"
  3. data-srcset="path/to/small.jpg 700w,
  4.              path/to/medium.jpg 1000w,
  5.              path/to/large.jpg 1200w"
  6. data-sizes="(max-width: 75em) 100vw,
  7.             75em"
  8. >
  9.   <img src="path/to/thumbnail.jpg" alt="">
  10. </a>
  11. ```

Localization


If you need localization, you can import the language module and set it as an option.

  1. ```js
  2. import de from 'parvus/src/l10n/de'

  3. const prvs = new Parvus({
  4.   l10n: de
  5. })
  6. ```

Options


You can pass an object with custom options as an argument when initializing Parvus.

  1. ```js
  2. const prvs = new Parvus({
  3.   // Clicking outside closes Parvus
  4.   docClose: false
  5. })
  6. ```

The following options are available:

  1. ```js
  2. {
  3.   // Selector for elements that trigger Parvus
  4.   selector: '.lightbox',

  5.   // Selector for a group of elements that should be combined as a gallery. Overrides the `data-group` attribute.
  6.   gallerySelector: null,

  7.   // Display captions if available
  8.   captions: true,

  9.   // Selector for the element where the caption is displayed. Use "self" for the `a` tag itself.
  10.   captionsSelector: 'self',

  11.   // Attribute to get the caption from
  12.   captionsAttribute: 'data-caption',

  13.   // Clicking outside closes Parvus
  14.   docClose: true,

  15.   // Closing Parvus by swiping up/down
  16.   swipeClose: true,

  17.   // Accepting mouse events like touch events (click and drag to change slides)
  18.   simulateTouch: true,

  19.   // Touch dragging threshold (in pixels)
  20.   threshold: 100,

  21.   // Setting focus back to the trigger element after closing Parvus
  22.   backFocus: true,

  23.   // Browser scrollbar visibility
  24.   hideScrollbar: true,

  25.   // Duration of transition effects in milliseconds (ms)
  26.   transitionDuration: 300,

  27.   // Timing function of the transition effects
  28.   transitionTimingFunction: 'cubic-bezier(0.2, 0, 0.2, 1)',

  29.   // Icons
  30.   lightboxIndicatorIcon: '',
  31.   previousButtonIcon: '',
  32.   nextButtonIcon: '',
  33.   closeButtonIcon: '',

  34.   // Localization of strings
  35.   l10n: en
  36. }
  37. ```

API


Parvus provides the following API functions:

FunctionDescription
------
`open(element)`Open
`close()`Close
`previous()`Show
`next()`Show
`select(index)`Select
`add(element)`Add
`remove(element)`Remove
`destroy()`Destroy
`isOpen()`Check
`currentIndex()`Get

Events


You can bind and unbind events using the .on() and .off() methods.

  1. ```js
  2. const prvs = new Parvus()

  3. const listener = function listener () {
  4.   console.log('eventName happened')
  5. }

  6. // bind event listener
  7. prvs.on(eventName, listener)

  8. // unbind event listener
  9. prvs.off(eventName, listener)
  10. ```

The following events are available:

eventNameDescription
------
`open`Triggered
`select`Triggered
`close`Triggered
`destroy`Triggered

Except for the destroy event, you can access the current source element using the event.detail.source property.

  1. ```js
  2. prvs.on('open', function (event) {
  3.   console.log(event.detail.source);
  4. })
  5. ```

Browser Support


Parvus is supported on the latest versions of the following browsers:

- Chrome
- Edge
- Firefox
- Safari