glightbox vs lightbox2 vs lightgallery.js vs magnific-popup vs photoswipe vs viewerjs
Image and Video Lightbox Libraries
glightboxlightbox2lightgallery.jsmagnific-popupphotoswipeviewerjsSimilar Packages:

Image and Video Lightbox Libraries

Lightbox libraries are JavaScript plugins that create a modal overlay to display images, videos, or other content in a focused manner, dimming the background to enhance the viewing experience. They are commonly used in web applications to showcase media in a more interactive and visually appealing way, allowing users to navigate through a gallery without leaving the current page. These libraries often support features like image zoom, captions, fullscreen mode, and responsive design, making them versatile tools for enhancing multimedia presentations on websites.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
glightbox02,442401 kB45a year agoMIT
lightbox206,376946 kB4821 days ago-
lightgallery.js05,332611 kB4-GPLv3
magnific-popup011,3571.05 MB6782 years agoMIT
photoswipe025,1181.21 MB1632 years agoMIT
viewerjs08,196504 kB37a year agoMIT

Feature Comparison: glightbox vs lightbox2 vs lightgallery.js vs magnific-popup vs photoswipe vs viewerjs

Size and Performance

  • glightbox:

    glightbox is lightweight (around 10KB minified) and optimized for performance, making it suitable for modern web applications where load time and resource usage are critical.

  • lightbox2:

    lightbox2 is also lightweight (approximately 5KB minified), ensuring quick loading times. Its simplicity contributes to low resource consumption, making it ideal for small to medium-sized projects.

  • lightgallery.js:

    lightgallery.js is larger than some alternatives (about 30KB minified) due to its feature-rich nature. However, it is modular, allowing developers to include only the components they need, which helps manage performance.

  • magnific-popup:

    magnific-popup is lightweight (around 8KB minified) and designed for fast performance. Its focus on simplicity and efficiency makes it a great choice for projects that prioritize speed.

  • photoswipe:

    photoswipe is relatively lightweight (about 15KB minified) for a feature-rich lightbox, but its emphasis on high-quality image handling and zooming may require additional resources for large galleries.

  • viewerjs:

    viewerjs is a lightweight library (approximately 10KB minified) that provides efficient image viewing with minimal impact on page load times, making it suitable for performance-sensitive applications.

Customization

  • glightbox:

    glightbox offers extensive customization options, including the ability to style the lightbox using CSS, customize animations, and add your own content (like captions and buttons) through its API.

  • lightbox2:

    lightbox2 allows for basic customization, such as changing the overlay color, image captions, and navigation buttons. It is easy to modify the styles with CSS, but it does not provide a wide range of configurable options out of the box.

  • lightgallery.js:

    lightgallery.js is highly customizable, with a modular architecture that allows developers to enable or disable features as needed. It supports custom themes, CSS styling, and has a rich API for programmatic control.

  • magnific-popup:

    magnific-popup provides a high level of customization, allowing developers to change almost every aspect of the lightbox, from animations and transitions to button styles and content types, all while keeping the code clean and manageable.

  • photoswipe:

    photoswipe is designed for customization, with a focus on providing a flexible API and structure that allows developers to easily modify the lightbox’s appearance and behavior, including custom animations, controls, and layouts.

  • viewerjs:

    viewerjs allows for some customization, particularly in terms of styling the viewer and configuring options like zoom levels, rotation, and fullscreen behavior. However, it is more limited compared to some other libraries in terms of extensibility.

Gallery Support

  • glightbox:

    glightbox supports image and video galleries, allowing users to navigate through multiple items seamlessly. It also supports grouping items for a more organized gallery experience.

  • lightbox2:

    lightbox2 supports basic image galleries by grouping multiple images within the same link. It provides simple navigation between images, but it lacks advanced features like video support or dynamic content.

  • lightgallery.js:

    lightgallery.js offers robust support for image and video galleries, including features like lazy loading, thumbnail navigation, and social sharing. It is highly versatile and supports various content types, making it suitable for complex galleries.

  • magnific-popup:

    magnific-popup supports image and video galleries, allowing for smooth transitions between multiple items. It also supports inline content and AJAX, making it versatile for different types of galleries.

  • photoswipe:

    photoswipe is designed primarily for image galleries, offering a smooth and responsive experience with touch support. It excels in handling large images and provides a zoom feature, but it is less focused on video or inline content.

  • viewerjs:

    viewerjs is primarily focused on image viewing and provides basic support for galleries by allowing users to navigate through a set of images. However, it does not support video or advanced gallery features.

Accessibility

  • glightbox:

    glightbox is designed with accessibility in mind, providing keyboard navigation, ARIA attributes, and support for screen readers, making it a good choice for inclusive web applications.

  • lightbox2:

    lightbox2 includes basic accessibility features such as keyboard navigation and ARIA roles, but it may require additional enhancements to fully meet modern accessibility standards.

  • lightgallery.js:

    lightgallery.js is built with accessibility in mind, offering keyboard navigation, ARIA support, and customizable focus management, making it a good choice for accessible web applications.

  • magnific-popup:

    magnific-popup provides basic accessibility features, including keyboard navigation and ARIA attributes. However, developers may need to implement additional accessibility features to ensure full compliance.

  • photoswipe:

    photoswipe is designed with accessibility in mind, featuring keyboard navigation, ARIA roles, and support for screen readers. It is one of the more accessible lightbox options available, but like all tools, it benefits from additional testing and refinement.

  • viewerjs:

    viewerjs includes basic accessibility features such as keyboard navigation and support for screen readers, but it may not be as fully featured in terms of accessibility as some other libraries.

Ease of Use: Code Examples

  • glightbox:

    Simple usage of glightbox

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/glightbox/dist/css/glightbox.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/glightbox/dist/js/glightbox.min.js"></script>
    <div class="glightbox" data-glightbox="title: My Image; description: This is an image; video: https://www.youtube.com/watch?v=dQw4w9WgXcQ">
      <img src="image.jpg" alt="My Image" />
    </div>
    <script>
      const lightbox = GLightbox();
    </script>
    
  • lightbox2:

    Simple usage of lightbox2

    <link href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/css/lightbox.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/js/lightbox.min.js"></script>
    <a href="image.jpg" data-lightbox="gallery" data-title="My Image"> <img src="thumbnail.jpg" alt="My Image" /> </a>
    
  • lightgallery.js:

    Simple usage of lightgallery.js

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery.js/dist/css/lightgallery.css" />
    <script src="https://cdn.jsdelivr.net/npm/lightgallery.js/dist/lightgallery.min.js"></script>
    <div id="lightgallery">
      <a href="image1.jpg" data-lg-size="1024-768" data-lg-title="Image 1">
        <img src="thumbnail1.jpg" />
      </a>
      <a href="image2.jpg" data-lg-size="1024-768" data-lg-title="Image 2">
        <img src="thumbnail2.jpg" />
      </a>
    </div>
    <script>
      lightGallery(document.getElementById('lightgallery'));
    </script>
    
  • magnific-popup:

    Simple usage of magnific-popup

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
    <a href="image.jpg" class="popup-image"> <img src="thumbnail.jpg" alt="Image" /> </a>
    <script>
      $(document).ready(function () {
        $('.popup-image').magnificPopup({
          type: 'image',
          gallery: { enabled: true },
        });
      });
    </script>
    
  • photoswipe:

    Simple usage of photoswipe

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/default-skin/default-skin.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>
    <div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">
      <figure itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
        <a href="large-image.jpg" itemprop="contentUrl" data-size="1024x768">
          <img src="thumbnail.jpg" itemprop="thumbnail" alt="Image description" />
        </a>
        <figcaption itemprop="caption description">Image caption</figcaption>
      </figure>
    </div>
    
  • viewerjs:

    Simple usage of viewerjs

    <link rel="stylesheet" href="https://unpkg.com/viewerjs/dist/viewer.css" />
    <script src="https://unpkg.com/viewerjs/dist/viewer.js"></script>
    <div id="image-viewer">
      <img src="image.jpg" alt="Image" />
    </div>
    <script>
      const viewer = new Viewer(document.getElementById('image-viewer'));
    </script>
    

How to Choose: glightbox vs lightbox2 vs lightgallery.js vs magnific-popup vs photoswipe vs viewerjs

  • glightbox:

    Choose glightbox if you need a modern, lightweight lightbox with support for images, videos, and iframes, along with a simple API and customizable styles.

  • lightbox2:

    Select lightbox2 for a straightforward, no-frills lightbox solution that is easy to implement and customize, ideal for projects that require basic image viewing functionality.

  • lightgallery.js:

    Opt for lightgallery.js if you need a feature-rich, modular lightbox with support for image galleries, videos, and advanced features like lazy loading and social sharing.

  • magnific-popup:

    Pick magnific-popup for a highly customizable and performance-oriented lightbox that supports images, videos, and inline content, with a focus on speed and simplicity.

  • photoswipe:

    Choose photoswipe if you require a touch-friendly, responsive lightbox with a focus on image galleries, zooming capabilities, and a clean, modern design.

  • viewerjs:

    Select viewerjs for a lightweight image viewer that provides a simple interface for viewing images with zoom, rotate, and fullscreen features, making it ideal for applications that need a minimalist design.

README for glightbox

GLightbox

GLightbox is a pure javascript lightbox. It can display images, iframes, inline content and videos with optional autoplay for YouTube, Vimeo and even self hosted videos.

Features

  • Small - only 11KB Gzipped
  • Fast and Responsive - works with any screen size
  • Gallery Support - Create multiple galleries
  • Responsive Images Support - Let the browser use the optimal image for the current screen resolution
  • Video Support - Youtube, Vimeo and self hosted videos with autoplay
  • Inline content support - display any inline content
  • Iframe support - need to embed an iframe? no problem
  • Keyboard Navigation - esc, arrows keys, tab and enter is all you need
  • Touch Navigation - mobile touch events
  • Zoomable images - zoom and drag images on mobile and desktop
  • API - control the lightbox with the provided methods
  • Themeable - create your skin or modify the animations with some minor css changes

Live Demo

You can check the live demo right here

Usage

$ npm install glightbox
# OR
$ yarn add glightbox
# OR
$ bower install glightbox
// Using ESM specification
import '/path/to/glightbox.js';

// Using a bundler like webpack
import GLightbox from 'glightbox';

Or manually download and link glightbox.min.js in your HTML:

<link rel="stylesheet" href="dist/css/glightbox.css" />
<script src="dist/js/glightbox.min.js"></script>

<!-- USING A CDN -->

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/glightbox/dist/css/glightbox.min.css" />
<script src="https://cdn.jsdelivr.net/gh/mcstudios/glightbox/dist/js/glightbox.min.js"></script>

<script type="text/javascript">
  const lightbox = GLightbox({ ...options });
</script>

<!-- USING ES MODULES -->

<script type="module">
  import 'https://cdn.jsdelivr.net/gh/mcstudios/glightbox/dist/js/glightbox.min.js';

  const lightbox = GLightbox({ ...options });
</script>

Examples

<!-- Simple image -->
<a href="https://github.com/biati-digital/glightbox/blob/HEAD/large.jpg" class="glightbox">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>

<!-- Video -->
<a href="https://vimeo.com/115041822" class="glightbox2">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>

<!-- Gallery -->
<a href="https://github.com/biati-digital/glightbox/blob/HEAD/large.jpg" class="glightbox3" data-gallery="gallery1">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>
<a href="https://github.com/biati-digital/glightbox/blob/HEAD/video.mp4" class="glightbox3" data-gallery="gallery1">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>

<!-- Simple Description -->
<a href="https://github.com/biati-digital/glightbox/blob/HEAD/large.jpg" class="glightbox4" data-glightbox="title: My title; description: this is the slide description">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>

<!-- Advanced Description -->
<a href="https://github.com/biati-digital/glightbox/blob/HEAD/large.jpg" class="glightbox5" data-glightbox="title: My title; description: .custom-desc1">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>

<div class="glightbox-desc custom-desc1">
  <p>The content of this div will be used as the slide description</p>
  <p>You can add links and any HTML you want</p>
</div>

<!-- URL with no extension -->
<a href="https://picsum.photos/1200/800" data-glightbox="type: image">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>
<!-- OR using multiple data attributes -->
<a href="https://picsum.photos/1200/800" data-type="image">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>

<!-- Using responsive images: specify sizes and srcset through data attributes in the
     same way you would with the img tag.
     See: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images -->
<a href="https://github.com/biati-digital/glightbox/blob/HEAD/default.jpg" class="glightbox6" data-title="Responsive example"
data-description="Your browser will choose the optimal image for the resolution"
data-sizes="(max-width: 600px) 480px, 800px"
data-srcset="img480.jpg 480w, img800.jpg 800w">
  <img src="https://raw.githubusercontent.com/biati-digital/glightbox/HEAD/small.jpg" alt="image" />
</a>

Slide Options

You can specify some options to each individual slide, the available options are:

  • title
  • alt
  • description
  • descPosition
  • type
  • effect
  • width
  • height
  • zoomable
  • draggable
<!-- One line config -->
<a href="https://github.com/biati-digital/glightbox/blob/HEAD/large.jpg" data-glightbox="title: Your title; description: description here; descPosition: left; type: image; effect: fade; width: 900px; height: auto; zoomable: true; draggable: true;"></a>

<!-- Multiple data attributes / You can use the options as separated data attributes -->
<a
  href="https://github.com/biati-digital/glightbox/blob/HEAD/large.jpg"
  data-title="My title"
  data-description="description here"
  data-desc-position="right"
  data-type="image"
  data-effect="fade"
  data-width="900px"
  data-height="auto"
  data-zoomable="true"
  data-draggable="true"
></a>

Lightbox Options

Example use of the options.

const lightbox = GLightbox({
    touchNavigation: true,
    loop: true,
    autoplayVideos: true
});

// Instead of using a selector, define the gallery elements
const myGallery = GLightbox({
    elements: [
        {
            'href': 'https://picsum.photos/1200/800',
            'type': 'image',
            'title': 'My Title',
            'description': 'Example',
        },
        {
            'href': 'https://picsum.photos/1200/800',
            'type': 'image',
            'alt': 'image text alternatives'
        },
        {
            'href': 'https://www.youtube.com/watch?v=Ga6RYejo6Hk',
            'type': 'video',
            'source': 'youtube', //vimeo, youtube or local
            'width': 900,
        },
        {
            'content': '<p>This will append some html inside the slide</p>' // read more in the API section
        },
        {
            'content': document.getElementById('inline-example') // this will append a node inside the slide
        },
    ],
    autoplayVideos: true,
});
myGallery.open();

// If later you need to modify the elements you can use setElements
myGallery.setElements([...]);
OptionTypeDefaultDescription
selectorstring.glightboxName of the selector for example '.glightbox' or 'data-glightbox' or '*[data-glightbox]'
elementsarraynullInstead of passing a selector you can pass all the items that you want in the gallery.
skinstringcleanName of the skin, it will add a class to the lightbox so you can style it with css.
openEffectstringzoomName of the effect on lightbox open. (zoom, fade, none)
closeEffectstringzoomName of the effect on lightbox close. (zoom, fade, none)
slideEffectstringslideName of the effect on slide change. (slide, fade, zoom, none)
moreTextstringSee moreMore text for descriptions on mobile devices.
moreLengthnumber60Number of characters to display on the description before adding the moreText link (only for mobiles), if 0 it will display the entire description.
closeButtonbooleantrueShow or hide the close button.
touchNavigationbooleantrueEnable or disable the touch navigation (swipe).
touchFollowAxisbooleantrueImage follow axis when dragging on mobile.
keyboardNavigationbooleantrueEnable or disable the keyboard navigation.
closeOnOutsideClickbooleantrueClose the lightbox when clicking outside the active slide.
startAtnumber0Start lightbox at defined index.
widthnumber900pxDefault width for inline elements and iframes, you can define a specific size on each slide. You can use any unit for example 90% or 100vw for full width
heightnumber506pxDefault height for inline elements and iframes, you can define a specific size on each slide.You can use any unit for example 90% or 100vh For inline elements you can set the height to auto.
videosWidthnumber960pxDefault width for videos. Videos are responsive so height is not required. The width can be in px % or even vw for example, 500px, 90% or 100vw for full width videos
descPositionstringbottomGlobal position for slides description, you can define a specific position on each slide (bottom, top, left, right).
loopbooleanfalseLoop slides on end.
zoomablebooleantrueEnable or disable zoomable images you can also use data-zoomable="false" on individual nodes.
draggablebooleantrueEnable or disable mouse drag to go prev and next slide (only images and inline content), you can also use data-draggable="false" on individual nodes.
dragToleranceXnumber40Used with draggable. Number of pixels the user has to drag to go to prev or next slide.
dragToleranceYnumber65Used with draggable. Number of pixels the user has to drag up or down to close the lightbox (Set 0 to disable vertical drag).
dragAutoSnapbooleanfalseIf true the slide will automatically change to prev/next or close if dragToleranceX or dragToleranceY is reached, otherwise it will wait till the mouse is released.
preloadbooleantrueEnable or disable preloading.
svgobject{}Set your own svg icons.
cssEfectsobject'See animations'Define or adjust lightbox animations. See the Animations section in the README.
lightboxHTMLstring'See themes'You can completely change the html of GLightbox. See the Themeable section in the README.
slideHTMLstring'See themes'You can completely change the html of the individual slide. See the Themeable section in the README.
autoplayVideosbooleantrueAutoplay videos on open.
autofocusVideosbooleanfalseIf true video will be focused on play to allow keyboard sortcuts for the player, this will deactivate prev and next arrows to change slide so use it only if you know what you are doing.
plyrobject{}View video player options.

Events

You can listen for events using your GLightbox instance (see example under the table). You can use the on() API method or once().

const lightbox = GLightbox();
lightbox.on('open', () => {
  // Do something
});

lightbox.once('slide_changed', () => {
  // Do something just one time
});
Event TypeDescription
openProvide a function when the lightbox is opened.
closeProvide a function when the lightbox is closed.
slide_before_changeTrigger a function before the slide is changed.
slide_changedTrigger a function after the slide is changed.
slide_before_loadTrigger a function before a slide is loaded for the first time, the function will only be called once
slide_after_loadTrigger a function after a slide is loaded and it's content is set for the first time, the function will only be called once
slide_insertedTrigger a function after a slide is inserted using insertSlide.
slide_removedTrigger a function after a slide is removed`
const lightbox = GLightbox();
lightbox.on('slide_before_change', ({ prev, current }) => {
  console.log('Prev slide', prev);
  console.log('Current slide', current);

  // Prev and current are objects that contain the following data
  const { slideIndex, slideNode, slideConfig, player, trigger } = current;

  // slideIndex - the slide index
  // slideNode - the node you can modify
  // slideConfig - will contain the configuration of the slide like title, description, etc.
  // player - the slide player if it exists otherwise will return false
  // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically
});

lightbox.on('slide_changed', ({ prev, current }) => {
  console.log('Prev slide', prev);
  console.log('Current slide', current);

  // Prev and current are objects that contain the following data
  const { slideIndex, slideNode, slideConfig, player, trigger } = current;

  // slideIndex - the slide index
  // slideNode - the node you can modify
  // slideConfig - will contain the configuration of the slide like title, description, etc.
  // player - the slide player if it exists otherwise will return false
  // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically

  if (player) {
    if (!player.ready) {
      // If player is not ready
      player.on('ready', (event) => {
        // Do something when video is ready
      });
    }

    player.on('play', (event) => {
      console.log('Started play');
    });

    player.on('volumechange', (event) => {
      console.log('Volume change');
    });

    player.on('ended', (event) => {
      console.log('Video ended');
    });
  }
});

// Useful to modify the slide
// before it's content is added
lightbox.on('slide_before_load', (data) => {
  // data is an object that contain the following
  const { slideIndex, slideNode, slideConfig, player, trigger } = data;

  // slideIndex - the slide index
  // slideNode - the node you can modify
  // slideConfig - will contain the configuration of the slide like title, description, etc.
  // player - the slide player if it exists otherwise will return false
  // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically
});

// Useful to execute scripts that depends
// on the slide to be ready with all it's content
// and already has a height
// data will contain all the info about the slide
lightbox.on('slide_after_load', (data) => {
  // data is an object that contain the following
  const { slideIndex, slideNode, slideConfig, player, trigger } = data;

  // slideIndex - the slide index
  // slideNode - the node you can modify
  // slideConfig - will contain the configuration of the slide like title, description, etc.
  // player - the slide player if it exists otherwise will return false
  // trigger - this will contain the element that triggers this slide, this can be a link, a button, etc in your HTML, it can be null if the elements in the gallery were set dinamically
});

// Trigger a function when a slide is inserted
lightbox.on('slide_inserted', (data) => {
  // data is an object that contain the following
  const { slideIndex, slideNode, slideConfig, player, trigger } = data;

  // slideIndex - the slide index
  // slideNode - the node you can modify
  // slideConfig - will contain the configuration of the slide like title, description, etc.
  // player - the slide player if it exists otherwise will return false
  // trigger - null
});

// Trigger a function when a slide is removed
lightbox.on('slide_removed', (index) => {
  // index is the position of the element in the gallery
});

Video player

GLightbox includes "Plyr" the best player out there, you can pass any Plyr option to the player, view all available options here Plyr options. GLightbox will only inject the player library if required and only when the lightbox is opened.

Internet Explorer 11. If you need support for this browser you need to set the js url to use the polyfilled version. This is not the default because IE11 is ancient and we need to let it die.

Autoplay for mobile/tablet

Please note, autoplay is blocked in some browsers, there’s nothing we can do to change that unfortunately, the browser will decide if your video can be autoplayed. Please do not post issues about this, instead inform yourself about this topic:

they decide if a video can be autoplayed based in a few rules

plyr: {
    js: 'https://cdn.plyr.io/3.6.2/plyr.polyfilled.js',
    ....
const lightbox = GLightbox({
  plyr: {
    css: 'https://cdn.plyr.io/3.5.6/plyr.css', // Default not required to include
    js: 'https://cdn.plyr.io/3.5.6/plyr.js', // Default not required to include
    config: {
      ratio: '16:9', // or '4:3'
      muted: false,
      hideControls: true,
      youtube: {
        noCookie: true,
        rel: 0,
        showinfo: 0,
        iv_load_policy: 3
      },
      vimeo: {
        byline: false,
        portrait: false,
        title: false,
        speed: true,
        transparent: false
      }
    }
  }
});

API

There are methods, setters and getters on a GLightbox object. The easiest way to access the GLightbox object is to set the return value from your call to a variable. For example:

const lightbox = GLightbox({ ...options });

Methods

Example method use:

lightbox.nextSlide(); // Go to next slide
lightbox.close(); // Close the lightbox
OptionParametersDescription
opennodeOpen the lightbox, you can optionally pass a node.
openAtnumberOpen at specific index.
close-Close the lightbox.
reload-Reload the lightbox, after inserting content with ajax.
destroy-Destroy and remove all attached events.
prevSlide-Go to the previous slide.
nextSlide-Go to the next slide.
goToSlidenumberIndex of the slide.
insertSlideobject, indexInsert a slide at the specified index.
removeSlideindexRemove slide at the specified index.
getActiveSlide-Get active slide. It will return the active node.
getActiveSlideIndex-Get active slide. It will return the active slide index.
slidePlayerPlaynumberPlay video in the specified slide.
slidePlayerPausenumberPause video in the specified slide.
getSlidePlayerInstancenode, indexGet the player instance of the specified slide.
getAllPlayers-Get all players instance.
setElements[]Update the lightbox gallery elements.
onstring, functionSet an event listener. See Events section
oncestring, functionSet an event listener that will be triggered only once. See Events section
// Example set custom gallery items
// This overwrites all the items in the gallery
lightbox.setElements([
  {
    'href': 'https://picsum.photos/1200/800',
    'type': 'image' // Type is only required if GLightbox fails to know what kind of content should display
  },
  {
    'href': 'https://www.youtube.com/watch?v=Ga6RYejo6Hk',
    'type': 'video', // Type is only required if GLightbox fails to know what kind of content should display
    'width': '900px',
  },
  {
    'content': '<p>some html to append in the slide</p>',
    'width': '900px',
  }
]);


// Insert a single slide at the end of all the items,
lightbox.insertSlide({
    href: 'video url...',
    width: '90vw'
});

// Insert a single slide at index 2 or pass 0 to add it at the start
lightbox.insertSlide({
    href: 'video url...',
    width: '90vw'
}, 2);

// You can insert a slide with a defined html
lightbox.insertSlide({
    content: '<p>some html to append in the slide</p>',
    width: '90vw'
}, 2);

// Or if you prefer you can pass a node
// and it will be inserted in the slide
lightbox.insertSlide({
    content: document.getElementById('inline-example'),
    width: '90vw'
}, 2);

// Remove the slide at index 2
lightbox.removeSlide(2);

// Open the lightbox
lightbox.open();

// You can also open the lightbox at a specific index
lightbox.openAt(2);

// So imagine that you are making an ajax request that returns some html
// You can create an empty instance and append the content once is returned

const ajaxExample = GLightbox({ selector: null }); // or you can set the selector empty selector: ''

doAjaxCall({...}).then(response => {
    ajaxExample.insertSlide({
        width: '500px',
        content: response.html
    });
    ajaxExample.open();
})

// Or you could use the set elements method to empty all the slides if any

doAjaxCall({...}).then(response => {
    ajaxExample.setElements([
      {
        content: response.html
      }
    ]);
    ajaxExample.open();
})

Animations

Animations are created with CSS, each effect has an in and out value and they are used to attach the correct classes to the node.

For example if you are using

const glightbox = GLightbox({
  openEffect: 'zoom',
  closeEffect: 'fade',
  cssEfects: {
    // This are some of the animations included, no need to overwrite
    fade: { in: 'fadeIn', out: 'fadeOut' },
    zoom: { in: 'zoomIn', out: 'zoomOut' }
  }
});

The open effect will use cssEfects.zoom.in and will add the class gzoomIn, if you take a look at the CSS you'll see:

.gzoomIn {
    animation: gzoomIn .5s ease;
}

@keyframes gzoomIn {
    from {
        opacity: 0;
        transform: scale3d(.3, .3, .3);
    }
    to {
        opacity: 1;
    }
}

Adding a custom animation

You can create any animation you want, you can find some inspiration in the Animate.css library, for example you can add the bounce animation like this:

const glightbox = GLightbox({
  openEffect: 'bounce', // Define that we want the bounce animation on open
  cssEfects: {
    // register our new animation
    bounce: { in: 'bounceIn', out: 'bounceOut' }
  }
});
/*A g will be appended to the animation name so bounceIn will become gbounceIn */
.gbounceIn {
  animation: bounceIn 1.3s ease;
}

@keyframes bounceIn {
  from,
  20%,
  40%,
  60%,
  80%,
  to {
    animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  }

  0% {
    opacity: 0;
    transform: scale3d(0.3, 0.3, 0.3);
  }

  20% {
    transform: scale3d(1.1, 1.1, 1.1);
  }

  40% {
    transform: scale3d(0.9, 0.9, 0.9);
  }

  60% {
    opacity: 1;
    transform: scale3d(1.03, 1.03, 1.03);
  }

  80% {
    transform: scale3d(0.97, 0.97, 0.97);
  }

  to {
    opacity: 1;
    transform: scale3d(1, 1, 1);
  }
}

Themeable

You can completely customize the structure of GLightbox and use CSS to change any part you want.

const customLightboxHTML = `<div id="glightbox-body" class="glightbox-container">
    <div class="gloader visible"></div>
    <div class="goverlay"></div>
    <div class="gcontainer">
    <div id="glightbox-slider" class="gslider"></div>
    <button class="gnext gbtn" tabindex="0" aria-label="Next" data-customattribute="example">{nextSVG}</button>
    <button class="gprev gbtn" tabindex="1" aria-label="Previous">{prevSVG}</button>
    <button class="gclose gbtn" tabindex="2" aria-label="Close">{closeSVG}</button>
</div>
</div>`;

let customSlideHTML = `<div class="gslide">
    <div class="gslide-inner-content">
        <div class="ginner-container">
            <div class="gslide-media">
            </div>
            <div class="gslide-description">
                <div class="gdesc-inner">
                    <h4 class="gslide-title"></h4>
                    <div class="gslide-desc"></div>
                </div>
            </div>
        </div>
    </div>
</div>`;

const glightbox = GLightbox({
  lightboxHTML: customLightboxHTML,
  slideHTML: customSlideHTML,
  skin: 'supercool'
});

You can also define a skin name and the lightbox will append the class name "glightbox-supercool" so you can customize it with CSS, this will leave a barebones structure so you can change the buttons appearance, etc.

Development

$ npm install
$ npm run watch

Browser Support

GLightbox was tested in the following browsers.

  • Safari
  • Mobile Safari
  • Opera
  • Edge
  • Firefox
  • Internet Explorer 11

It will work in any browser that supports CSS Flexbox

Contributing

Feel free to report any issues! If you wish to contribute by fixing a bug or implementing a new feature, please first read the CONTRIBUTING guide.

Donate

If you find this code useful, please consider a donation to keep this project growing, any amount is appreciated.

paypal

Support

We only provide support for bugs and feature requests, so please only post issues about this two topics, if you need help implementing GLightbox or you are just starting with HTML/CSS/Javascript please use stackoverlow, you'll be able to find more help there. This will help us to keep the issues related to the library and solve issues faster.

Changelog

Latest version vundefined

See the CHANGELOG.md file for details

License

This project is licensed under the MIT License - see the LICENSE.md file for details