Lightbox2 Documentation

repository·dev·Indexed 27 days ago

https://github.com/lokesh/lightbox2

A lightweight JavaScript library for creating image overlays on web pages. Version 2.12.0 supports both jQuery and standalone vanilla JavaScript usage via a UMD bundle. It features a public API for programmatic control (open, close, next, prev, destroy), custom lifecycle events, and theming via CSS custom properties. The library can be triggered via HTML attributes like data-lightbox or rel="lightbox".

Tokens
1.6K
Snippets
4
Records
15
Agent score
92%

What's inside Lightbox2

  1. Overview of Lightbox2

    dev
    Lightbox2 is a small JavaScript library used to overlay images on top of the current page. It is designed for easy setup and works across all modern browsers. Note that Lightbox2 is currently in maintenance mode; for a zero-dependency version with fluid animations built for touch, consider using Lightbox3.
  2. Set up local development for Lightbox2

    dev

    To set up a local development environment for Lightbox2, follow these steps:

    1. Install dependencies using npm install.
    2. Run linting/tests using npm test.
    3. Build the project using npm run build.
    4. Start a local server (for example, using npx serve .) and navigate to /examples/index.html to view the project in action.
    npm install
    npm test
    npm run build
    npx serve .
  3. Use Lightbox2 without jQuery

    dev

    Lightbox2 can now be used as a standalone vanilla JavaScript library without requiring jQuery. This is achieved by using the standard UMD bundle. If you require backwards compatibility with existing jQuery-based workflows, you can use the lightbox-plus-jquery.js bundle, which concatenates jQuery before the vanilla lightbox code.

    Key Benefits:

    • Reduced bundle size (removes 30KB+ of jQuery).
    • No dependency on jQuery for DOM manipulation or events.
    • The public API remains identical to previous versions.
  4. Configure Lightbox2 security and sanitization

    dev

    Lightbox2 includes a sanitizeTitle option to prevent XSS attacks when using user-generated content in captions.

    Note: In recent updates, sanitizeTitle defaults to true. If you rely on injecting raw HTML into your captions via the data-title attribute, you may need to explicitly configure this (though be aware of the security implications).

  5. Theme Lightbox2 using CSS Custom Properties

    dev

    To customize the appearance of Lightbox2 without overriding numerous specific selectors, you can use CSS custom properties (variables). The following variables are recommended for theming:

    • --lb-overlay-opacity: Controls the opacity of the lightbox overlay.
    • --lb-border-radius: Sets the border radius for the lightbox container.
    • --lb-image-border: Defines the border style for the displayed image.
    • --lb-transition-speed: Sets the duration for CSS transitions.
    :root {
      --lb-overlay-opacity: 0.8;
      --lb-border-radius: 3px;
      --lb-image-border: 4px solid white;
      --lb-transition-speed: 0.6s;
    }
  6. Configure Lightbox2 Animation Durations via CSS

    dev

    Lightbox2 uses CSS custom properties to control animation durations. You can override these in your global CSS to customize the feel of transitions.

    Available CSS variables:

    • --lb-fade-duration: Controls the fade duration for the overlay and lightbox.
    • --lb-image-fade-duration: Controls the fade duration for the image itself.
    • --lb-resize-duration: Controls the duration of the container resizing animations.
    :root {
      --lb-fade-duration: 600ms;
      --lb-image-fade-duration: 600ms;
      --lb-resize-duration: 700ms;
    }
  7. Listen to Lightbox2 jQuery custom events

    dev
    You can hook into the lightbox lifecycle by listening to specific jQuery custom events. This is useful for triggering UI updates or analytics when the user interacts with the gallery.
  8. Programmatic Control and Events (Planned for v2.x)

    dev

    Future updates to the v2.x branch aim to expose a public API for controlling the lightbox via JavaScript and emitting events for integration with other components (like analytics or routing).

    Planned Public Methods:

    • open(imageUrl, options): Opens the lightbox at a specific URL.
    • close(): Closes the lightbox.
    • next(): Navigates to the next image.
    • prev(): Navigates to the previous image.
    • destroy(): Removes the lightbox DOM and event listeners (essential for SPAs).

    Planned Events (via jQuery custom events):

    • lightbox:open
    • lightbox:close
    • lightbox:change
  9. Listen to Lightbox2 Custom Events

    dev

    Lightbox2 uses native CustomEvent for its lifecycle events. You can listen for these events on the document object to trigger custom logic when the lightbox state changes.

    Supported events:

    • lightbox:open
    • lightbox:close
    • lightbox:change
  10. Use the Lightbox2 Public API

    dev

    The public API for Lightbox2 is identical whether you are using the vanilla or jQuery-dependent version. You can control the lightbox programmatically using the following methods:

    • lightbox.open(url): Opens the lightbox with the specified image URL.
    • lightbox.close(): Closes the active lightbox.
    • lightbox.next(): Navigates to the next image in the set.
    • lightbox.prev(): Navigates to the previous image in the set.
    • lightbox.destroy(): Destroys the lightbox instance and cleans up event listeners.
    • lightbox.option(options): Updates lightbox configuration options.