Skip to content

Lightbox

The Lightbox component provides an elegant way to display images in a fullscreen overlay. It includes navigation controls for browsing through multiple images, with support for image titles and responsive sizing to ensure optimal viewing on any device.

This component requires:

  • Metro UI core
  • DOM library
<div data-role="lightbox">
<img src="image1-thumb.jpg" data-original="image1-full.jpg" alt="Image Title 1">
<img src="image2-thumb.jpg" data-original="image2-full.jpg" alt="Image Title 2">
<img src="image3-thumb.jpg" data-original="image3-full.jpg" alt="Image Title 3">
</div>
<div data-role="lightbox"
data-icon-close="✖"
data-icon-prev="◀"
data-icon-next="▶"
data-cls-close="bg-red fg-white"
data-cls-prev="bg-blue fg-white"
data-cls-next="bg-blue fg-white">
<img src="image1-thumb.jpg" data-original="image1-full.jpg" alt="Image Title 1">
<img src="image2-thumb.jpg" data-original="image2-full.jpg" alt="Image Title 2">
</div>
Metro.makePlugin('#my-gallery', 'lightbox', {
loop: true,
iconClose: '✖',
iconPrev: '◀',
iconNext: '▶'
});
ParameterTypeDefaultDescription
loopbooleantrueWhether to loop back to the first image after reaching the last one
sourcestring"img"Selector for items to include in the lightbox
iconClosestring"❌"HTML content for the close button
iconPrevstring"🡐"HTML content for the previous button
iconNextstring"🡒"HTML content for the next button
clsNextstring""Additional CSS class for the next button
clsPrevstring""Additional CSS class for the previous button
clsClosestring""Additional CSS class for the close button
clsImagestring""Additional CSS class for the image
clsImageContainerstring""Additional CSS class for the image container
clsImageWrapperstring""Additional CSS class for the image wrapper
clsLightboxstring""Additional CSS class for the lightbox container
onDrawImagefunctionMetro.noopCallback function triggered when an image is drawn in the lightbox
onLightboxCreatefunctionMetro.noopCallback function triggered when the lightbox is created

Opens the lightbox with the specified element.

var lightbox = Metro.getPlugin('#my-lightbox', 'lightbox');
lightbox.open(document.querySelector('#my-lightbox img:first-child'));

Closes the lightbox.

var lightbox = Metro.getPlugin('#my-lightbox', 'lightbox');
lightbox.close();

Shows the next image in the lightbox.

var lightbox = Metro.getPlugin('#my-lightbox', 'lightbox');
lightbox.next();

Shows the previous image in the lightbox.

var lightbox = Metro.getPlugin('#my-lightbox', 'lightbox');
lightbox.prev();

Destroys the lightbox component.

var lightbox = Metro.getPlugin('#my-lightbox', 'lightbox');
lightbox.destroy();
EventDescription
onDrawImageTriggered when an image is drawn in the lightbox
onLightboxCreateTriggered when the lightbox is created

The Lightbox component uses the following CSS classes for styling:

  • .lightbox-overlay - The overlay background
  • .lightbox - The main lightbox container
  • .lightbox__image - The image container
  • .lightbox__image-wrapper - The wrapper for the image
  • .lightbox__image-portrait - Applied to portrait orientation images
  • .lightbox__image-landscape - Applied to landscape orientation images
  • .lightbox__closer - The close button
  • .lightbox__prev - The previous button
  • .lightbox__next - The next button
<div data-role="lightbox">
<img src="image1-thumb.jpg" data-original="image1-full.jpg" alt="Image Title 1">
<img src="image2-thumb.jpg" data-original="image2-full.jpg" alt="Image Title 2">
<img src="image3-thumb.jpg" data-original="image3-full.jpg" alt="Image Title 3">
</div>
<div data-role="lightbox" data-icon-close="✖" data-icon-prev="◀" data-icon-next="▶">
<img src="image1-thumb.jpg" data-original="image1-full.jpg" alt="Image Title 1">
<img src="image2-thumb.jpg" data-original="image2-full.jpg" alt="Image Title 2">
</div>
<div data-role="lightbox"
data-cls-close="bg-red fg-white"
data-cls-prev="bg-blue fg-white"
data-cls-next="bg-blue fg-white"
data-cls-lightbox="custom-lightbox">
<img src="image1-thumb.jpg" data-original="image1-full.jpg" alt="Image Title 1">
<img src="image2-thumb.jpg" data-original="image2-full.jpg" alt="Image Title 2">
</div>
  1. Always provide alt attributes for images to serve as titles in the lightbox
  2. Use data-original to specify high-resolution images for the lightbox view
  3. For better performance with many images, consider using thumbnails for the gallery view
  4. Use custom icons and styling to match your application’s design language
  5. Consider accessibility by ensuring navigation controls are clearly visible