GLightbox Documentation

repository·master·Indexed 25 days ago

https://github.com/biati-digital/glightbox

A lightweight, pure JavaScript lightbox library (version 3.3.1) for displaying images, videos (YouTube, Vimeo, self-hosted), iframes, and inline content. It features high performance, responsiveness, and support for galleries, custom CSS animations, and dynamic content loading via AJAX. GLightbox integrates with Plyr for video playback and provides a comprehensive API for managing slides and event listeners.

Tokens
8K
Snippets
19
Records
19
Agent score
32%

What's inside GLightbox

  1. Configure and add custom CSS animations

    master

    GLightbox animations are driven by CSS classes. You define an effect (e.g., zoom) and map it to specific CSS animation names via the cssEfects option.

    When an effect is triggered, GLightbox appends a g prefix to the animation name you provided. For example, if you register zoom: { in: 'zoomIn' }, GLightbox will apply the class .gzoomIn to the element.

    const glightbox = GLightbox({
      openEffect: 'zoom',
      closeEffect: 'fade',
      cssEfects: {
        fade: { in: 'fadeIn', out: 'fadeOut' },
        zoom: { in: 'zoomIn', out: 'zoomOut' }
      }
    });
    
    // Adding a custom animation
    const glightbox = GLightbox({
      openEffect: 'bounce',
      cssEfects: {
        bounce: { in: 'bounceIn', out: 'bounceOut' }
      }
    });
  2. Import GLightbox in JavaScript

    master

    Depending on your environment, you can import GLightbox using ESM specification or via a bundler like Webpack.

    // Using ESM specification
    import '/path/to/glightbox.js';
    
    // Using a bundler like webpack
    import GLightbox from 'glightbox';
  3. Include GLightbox via CDN

    master

    For quick integration without a build step, you can link the CSS and JS files directly from a CDN in your HTML.

    <!-- 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>
  4. Customize GLightbox structure and skins

    master

    You can completely override the HTML structure of the lightbox container and the individual slides using lightboxHTML and slideHTML.

    Additionally, you can use the skin option to apply a custom class (e.g., skin: 'supercool') to the lightbox container, which results in the class .glightbox-supercool being added. This allows you to target specific styles for your custom theme.

    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'
    });
  5. Configure the Plyr video player

    master

    GLightbox uses Plyr for video playback. You can pass any Plyr option via the plyr.config key in the GLightbox initialization object. GLightbox only injects the Plyr library when a video slide is opened.

    Note on IE11: If you require Internet Explorer 11 support, you must manually provide the polyfilled version of the Plyr JS URL in the plyr.js property.

    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
          }
        }
      }
    });
  6. Create a simple image lightbox

    master

    To create a basic image lightbox, wrap an <img> tag in an <a> tag where the href points to the large image and the class is glightbox.

    <!-- Simple image -->
    <a href="large.jpg" class="glightbox">
      <img src="small.jpg" alt="image" />
    </a>
  7. Add titles and descriptions to slides

    master

    You can add metadata to slides using the data-glightbox attribute with a semicolon-separated string, or by using individual data attributes.

    <!-- Using data-glightbox attribute -->
    <a href="large.jpg" data-glightbox="title: My title; description: this is the slide description">
      <img src="small.jpg" alt="image" />
    </a>
    
    <!-- Using individual data attributes -->
    <a
      href="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>
  8. Handle AJAX content loading

    master

    To load content dynamically via AJAX, initialize GLightbox with an empty selector (selector: null or selector: ''). Once your data is fetched, use insertSlide or setElements to populate the lightbox before calling open() or openAt().

    // Create an empty instance
    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 use setElements to replace everything
    doAjaxCall({...}).then(response => {
        ajaxExample.setElements([
          {
            content: response.html
          }
        ]);
        ajaxExample.open();
    })
  9. Handle video player events within GLightbox

    master

    When a slide contains a video, the player object is available in the event data. You can attach listeners to the player to handle playback states like ready, play, volumechange, and ended.

    lightbox.on('slide_changed', ({ prev, current }) => {
      const { player } = current;
    
      if (player) {
        if (!player.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');
        });
      }
    });
  10. Create a gallery

    master

    To group multiple items into a single gallery, add the data-gallery attribute with a unique name to each link.

    <!-- Gallery -->
    <a href="large.jpg" class="glightbox3" data-gallery="gallery1">
      <img src="small.jpg" alt="image" />
    </a>
    <a href="video.mp4" class="glightbox3" data-gallery="gallery1">
      <img src="small.jpg" alt="image" />
    </a>