Modern JavaScript Lightbox with Zoom & Gallery – Zoomora
It automatically detects whether images can be zoomed based on their dimensions, implements pan-and-drag functionality for navigated zoomed content, and provides auto-hide controls for an immersive viewing experience.
| Key | Action |
|---|---|
← |
Previous image |
→ |
Next image |
Esc |
Close lightbox / Exit fullscreen |
Z |
Toggle zoom |
F |
Toggle fullscreen |
T |
Toggle thumbnails |
1. Install & download Zoomora with NPM.
# NPM $ npm install zoomora
2. Include Zoomora’s JavaScript and CSS files on your web page.
<link rel="stylesheet" href="dist/zoomora.css"> <script src="dist/zoomora.umd.min.js"></script>
3. Add the data-zoomora attribute to your image elements, assigning a unique group ID to create a gallery.
<!-- Image --> <img src="https://picsum.photos/seed/landscape1/800/600" alt="A random landscape image" data-src="https://picsum.photos/seed/landscape1/1600/1200" data-caption="A beautiful landscape scene." data-zoomora="demo-gallery" loading="lazy"> <!-- Video --> <img src="https://picsum.photos/seed/video_thumb/800/600" alt="YouTube Video Thumbnail" data-src="https://www.youtube.com/watch?v=2pg4QOqW-aI" data-type="video" data-caption="An embedded YouTube video." data-zoomora="demo-gallery" loading="lazy"> <!-- More Items Here -->
4. Initialize Zoomora and done.
document.addEventListener('DOMContentLoaded', function () {
new Zoomora({
gallery: 'demo-gallery'
});
}); 5. Customize the behavior and appearance of the lightbox.
selector: (String) The CSS selector for lightbox trigger elements. Default: '[data-zoomora]'.showCounter: (Boolean) Toggles the display of the image counter (e.g., “1 / 5”). Default: true.showThumbnails: (Boolean) Toggles the visibility of the thumbnail navigation strip. Default: true.showFullscreen: (Boolean) Toggles the fullscreen button. Default: true.showZoom: (Boolean) Toggles the zoom button. Default: true.transition: (String) Sets the transition effect between items, either ‘fade’ or ‘slide’. Default: 'fade'.useHref: (Boolean) If true, uses the href attribute for the media source instead of data-src. Default: false.maxZoomScale: (Number) Defines the maximum zoom level for an image. Default: 3.zoomStep: (Number) Controls the zoom increment when using the scroll wheel. Default: 0.1.animationDuration: (Number) The duration of transition animations in milliseconds. Default: 300.showAutoHideToggle: (Boolean) Toggles the button to enable or disable auto-hiding of controls. Default: true.autoHideDelay: (Number) The delay in milliseconds before controls automatically hide. Default: 3000.autoHideEnabled: (Boolean) Determines if the auto-hide feature is enabled by default. Default: false.onOpen: (Function) A callback function that executes when the lightbox opens. Default: null.onClose: (Function) A callback function that executes when the lightbox closes. Default: null.onNext: (Function) A callback that runs when navigating to the next item. Default: null.onPrev: (Function) A callback that runs when navigating to the previous item. Default: null.new Zoomora({
selector: '[data-zoomora]',
showCounter: true,
showThumbnails: true,
showFullscreen: true,
showZoom: true,
transition: 'fade',
useHref: false,
maxZoomScale: 3,
zoomStep: 0.1,
animationDuration: 300,
showAutoHideToggle: true,
autoHideDelay: 3000,
autoHideEnabled: false,
}); 6. Callback functions
new Zoomora({
onOpen: (element, index) => {
console.log('Opened lightbox at index:', index);
},
onClose: (index) => {
console.log('Closed lightbox, was viewing index:', index);
},
onNext: (index) => {
console.log('Navigated to next item:', index);
},
onPrev: (index) => {
console.log('Navigated to previous item:', index);
}
}); 7. API methods.
const lightbox = new Zoomora();
// Navigate
lightbox.next(); // Go to next item
lightbox.prev(); // Go to previous item
lightbox.goTo(2); // Go to specific index
// Controls
lightbox.open(element); // Open lightbox with element
lightbox.close(); // Close lightbox
lightbox.toggleZoom(); // Toggle zoom
lightbox.toggleFullscreen(); // Toggle fullscreen
lightbox.toggleThumbnails(); // Toggle thumbnails
lightbox.toggleAutoHide(); // Toggle auto-hide controls
// State
lightbox.isOpen(); // Check if open
lightbox.getCurrentIndex(); // Get current index
lightbox.getTotalItems(); // Get total items
// Management
lightbox.refresh(); // Refresh gallery items
lightbox.updateOptions({ // Update options
transition: 'slide'
});
lightbox.destroy(); // Clean up and remove 8. Override the default CSS variables to create your own styles.
/* Override CSS variables */:root {
--zoomora-overlay-bg: rgba(0, 0, 0, 0.95);
--zoomora-control-bg: rgba(255, 255, 255, 0.1);
--zoomora-control-hover: rgba(255, 255, 255, 0.2);
}
/* Custom styles */.zoomora {
/* Your custom styles */} 9. Some implementations require using anchor tags instead of direct image attributes. Set the useHref option to true and use the Zoomora.bind() method with a selector matching your anchor elements. This pattern works well for progressive enhancement scenarios where you want functional links that open images in a new tab when JavaScript fails to load.
const lightbox = Zoomora.bind('a[data-lightbox]', {
useHref: true
}); <a href="full-image.jpg" data-lightbox="gallery"> <img src="thumbnail.jpg" alt="Image"> </a>
Q: Can Zoomora handle dynamically added images after page load?
A: Yes. Call the refresh() method on your lightbox instance after adding new images to the DOM. This method rescans for elements matching your selector and attaches event listeners to new items. You can automate this by calling refresh in your dynamic content loading callbacks or after AJAX operations complete.
Q: How does zoom detection work for determining if an image is zoomable?
A: Zoomora compares the natural dimensions of an image against its displayed dimensions in the lightbox. If the image’s actual resolution exceeds what fits in the viewport, the library enables zoom controls. This prevents zoom functionality from appearing on images that are already displayed at full size or smaller, which would result in pixelation.
Q: What happens if a YouTube video URL is invalid or the video is deleted?
A: The library creates the iframe embed with the extracted video ID, but YouTube will display its standard error message within the iframe if the video is unavailable. Zoomora does not perform URL validation before creating embeds, so you need to validate YouTube URLs in your application code if you want to prevent loading invalid videos.
Q: Can I use multiple independent Zoomora instances with different configurations on the same page?
A: Yes. Create separate instances with different selector options to target different gallery groups. Each instance maintains its own configuration and state, so you can have one gallery with thumbnails enabled and another with thumbnails hidden, or use different transition effects for different content sections.
Q: Does the library handle images with different aspect ratios in the same gallery?
A: Zoomora adjusts the display size for each image independently to fit within the viewport while maintaining aspect ratio. When navigating between images with different dimensions, the lightbox resizes smoothly based on your configured animation duration. Thumbnail dimensions remain consistent regardless of source image aspect ratios.
Q: What happens to video playback when navigating away from a video item?
A: The library stops video playback when users navigate to a different gallery item or close the lightbox. For YouTube embeds, this occurs automatically when the iframe is removed from the DOM. Local video elements receive a pause command before being removed. This prevents videos from continuing to play audio in the background.
Q: How does Zoomora handle images that fail to load?
A: The library does not include built-in error handling for failed image loads. The browser’s default broken image icon will appear in the lightbox if a source URL returns a 404 or fails to load. Implement error handling by listening for error events on your image elements and either providing fallback sources or removing items from galleries that fail to load.
The post Modern JavaScript Lightbox with Zoom & Gallery – Zoomora appeared first on CSS Script.
AvalynxAutocomplete is a lightweight Bootstrap autocomplete component that converts text inputs into searchable, keyboard-navigable dropdown…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
MACOMB COUNTY, MI. (WOWO) A 40-year-old man convicted in a petition signature fraud scheme tied…
This website uses cookies.