Lightbox2 Documentation
repository·dev·Indexed 27 days ago
https://github.com/lokesh/lightbox2A 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".
What's inside Lightbox2
- 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.
Set up local development for Lightbox2
devTo set up a local development environment for Lightbox2, follow these steps:
- Install dependencies using
npm install. - Run linting/tests using
npm test. - Build the project using
npm run build. - Start a local server (for example, using
npx serve .) and navigate to/examples/index.htmlto view the project in action.
npm install npm test npm run build npx serve .- Install dependencies using
Use Lightbox2 without jQuery
devLightbox2 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.jsbundle, 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.
Configure Lightbox2 security and sanitization
devLightbox2 includes a
sanitizeTitleoption to prevent XSS attacks when using user-generated content in captions.Note: In recent updates,
sanitizeTitledefaults totrue. If you rely on injecting raw HTML into your captions via thedata-titleattribute, you may need to explicitly configure this (though be aware of the security implications).Theme Lightbox2 using CSS Custom Properties
devTo 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; }Configure Lightbox2 Animation Durations via CSS
devLightbox2 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; }Initialize Lightbox2
devLightbox2 can be used as a browser global, an AMD module, or via CommonJS (Node-like environments). It requires jQuery as a dependency. When used in the browser, it attaches towindow.lightbox.Trigger Lightbox via HTML attributes
devLightbox2 automatically listens for clicks on elements with specific attributes. To trigger the lightbox, adddata-lightboxorrel="lightbox"to your anchor (<a>) or area (<area>) tags.Listen to Lightbox2 jQuery custom events
devYou 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.Programmatic Control and Events (Planned for v2.x)
devFuture 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:openlightbox:closelightbox:change
Listen to Lightbox2 Custom Events
devLightbox2 uses native
CustomEventfor its lifecycle events. You can listen for these events on thedocumentobject to trigger custom logic when the lightbox state changes.Supported events:
lightbox:openlightbox:closelightbox:change
Use the Lightbox2 Public API
devThe 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.