baguetteBox.js
:zap: Simple and easy to use lightbox script written in pure JavaScript
Top Related Projects
JavaScript image gallery for mobile and desktop, modular, framework independent
A touchable jQuery lightbox
Full featured JavaScript image & video gallery. No dependencies
THE original Lightbox script (v2).
jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.
JavaScript image viewer.
Quick Overview
BaguetteBox.js is a lightweight and simple-to-use JavaScript lightbox library. It's designed to display images and galleries in a responsive, mobile-friendly overlay without any dependencies. The library is pure JavaScript, making it easy to integrate into various projects without the need for jQuery or other frameworks.
Pros
- Lightweight and fast, with no dependencies
- Responsive and mobile-friendly design
- Easy to set up and use with minimal configuration
- Supports touch gestures and keyboard navigation
Cons
- Limited advanced features compared to some larger lightbox libraries
- No built-in support for video content
- Customization options are somewhat limited
- Not actively maintained (last update was in 2021)
Code Examples
- Basic initialization:
baguetteBox.run('.gallery');
This code initializes BaguetteBox on all elements with the class 'gallery'.
- Custom options:
baguetteBox.run('.gallery', {
animation: 'fadeIn',
noScrollbars: true,
buttons: false
});
This example shows how to initialize BaguetteBox with custom options, such as animation type and hiding buttons.
- Programmatically show a specific image:
baguetteBox.show(2, '.gallery');
This code opens the lightbox and displays the third image (index 2) from the gallery.
Getting Started
- Include the BaguetteBox CSS and JS files in your HTML:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/baguettebox.js@1.11.1/dist/baguetteBox.min.css">
<script src="https://cdn.jsdelivr.net/npm/baguettebox.js@1.11.1/dist/baguetteBox.min.js" async></script>
- Create a gallery in your HTML:
<div class="gallery">
<a href="img/large-1.jpg"><img src="img/thumbs-1.jpg" alt="Image 1"></a>
<a href="img/large-2.jpg"><img src="img/thumbs-2.jpg" alt="Image 2"></a>
<a href="img/large-3.jpg"><img src="img/thumbs-3.jpg" alt="Image 3"></a>
</div>
- Initialize BaguetteBox in your JavaScript:
document.addEventListener('DOMContentLoaded', function() {
baguetteBox.run('.gallery');
});
That's it! Your gallery is now set up with BaguetteBox.js.
Competitor Comparisons
JavaScript image gallery for mobile and desktop, modular, framework independent
Pros of PhotoSwipe
- More feature-rich, offering advanced functionality like pinch-to-zoom and swipe gestures
- Highly customizable with extensive options and API
- Better performance for large galleries and high-resolution images
Cons of PhotoSwipe
- Larger file size and more complex setup compared to BaguetteBox.js
- Steeper learning curve due to its extensive features and options
- Requires more configuration for basic usage
Code Comparison
BaguetteBox.js (simple setup):
baguetteBox.run('.gallery');
PhotoSwipe (basic setup):
var pswpElement = document.querySelectorAll('.pswp')[0];
var items = [
{ src: 'image1.jpg', w: 1024, h: 768 },
{ src: 'image2.jpg', w: 1024, h: 768 }
];
var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
PhotoSwipe requires more initial setup and configuration, while BaguetteBox.js offers a simpler, more straightforward implementation for basic gallery needs. PhotoSwipe provides greater flexibility and features at the cost of complexity, making it suitable for more advanced use cases. BaguetteBox.js is ideal for simpler gallery requirements with minimal setup.
A touchable jQuery lightbox
Pros of Swipebox
- Supports touch gestures for mobile devices, allowing users to swipe between images
- Includes video support for YouTube and Vimeo
- Offers more customization options through its API
Cons of Swipebox
- Larger file size compared to BaguetteBox.js
- Less frequently updated, with the last commit being several years old
- Requires jQuery as a dependency, which may increase overhead
Code Comparison
BaguetteBox.js:
baguetteBox.run('.gallery', {
animation: 'fadeIn',
noScrollbars: true
});
Swipebox:
$('.gallery').swipebox({
useCSS: true,
hideBarsDelay: 0
});
Both libraries offer simple initialization, but Swipebox requires jQuery. BaguetteBox.js provides a more modern, standalone approach without dependencies. While Swipebox offers more features like video support and touch gestures, BaguetteBox.js focuses on simplicity and performance. The choice between the two depends on specific project requirements, such as the need for video support or touch gestures versus a lightweight, dependency-free solution.
Full featured JavaScript image & video gallery. No dependencies
Pros of lightgallery.js
- More feature-rich, including video support, thumbnails, and social sharing options
- Highly customizable with numerous options and API methods
- Better mobile touch support and responsive design
Cons of lightgallery.js
- Larger file size and potentially heavier on resources
- Steeper learning curve due to more complex configuration options
- May be overkill for simple image gallery needs
Code Comparison
baguetteBox.js:
baguetteBox.run('.gallery', {
animation: 'fadeIn',
noScrollbars: true
});
lightgallery.js:
lightGallery(document.getElementById('lightgallery'), {
mode: 'lg-fade',
cssEasing: 'cubic-bezier(0.25, 0, 0.25, 1)',
thumbnail: true,
animateThumb: false,
showThumbByDefault: false
});
The code comparison shows that lightgallery.js offers more configuration options out of the box, allowing for greater customization. However, this also means it requires more setup compared to the simpler baguetteBox.js implementation.
Both libraries serve the purpose of creating image galleries, but lightgallery.js is more suited for complex, feature-rich galleries, while baguetteBox.js is ideal for simpler, lightweight implementations.
THE original Lightbox script (v2).
Pros of Lightbox2
- More feature-rich, including support for image sets, captions, and keyboard navigation
- Highly customizable with extensive options and API
- Larger community and longer development history, potentially leading to better stability
Cons of Lightbox2
- Larger file size and potentially heavier performance impact
- Requires jQuery, which may not be ideal for all projects
- More complex setup and configuration compared to BaguetteBox.js
Code Comparison
BaguetteBox.js:
baguetteBox.run('.gallery');
Lightbox2:
lightbox.option({
'resizeDuration': 200,
'wrapAround': true
});
BaguetteBox.js offers a simpler setup with a single line of code, while Lightbox2 provides more customization options but requires additional configuration.
Both libraries serve similar purposes, but BaguetteBox.js focuses on simplicity and lightweight performance, while Lightbox2 offers more features and customization at the cost of increased complexity and file size. The choice between them depends on project requirements, with BaguetteBox.js being ideal for simpler implementations and Lightbox2 better suited for more complex use cases or projects already using jQuery.
jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.
Pros of Fancybox
- More feature-rich with advanced options like touch support, zooming, and thumbnails
- Responsive design with mobile-friendly controls
- Regular updates and active community support
Cons of Fancybox
- Larger file size due to more features, potentially impacting page load times
- Steeper learning curve for advanced customizations
- Requires jQuery, adding an additional dependency
Code Comparison
BaguetteBox.js:
baguetteBox.run('.gallery', {
animation: 'fadeIn',
noScrollbars: true
});
Fancybox:
$('[data-fancybox]').fancybox({
toolbar: false,
smallBtn: true,
iframe: {
preload: false
}
});
Summary
BaguetteBox.js is a lightweight, vanilla JavaScript lightbox with basic functionality, ideal for simple image galleries. Fancybox offers more advanced features and customization options but comes with a larger footprint and jQuery dependency. Choose BaguetteBox.js for simplicity and performance, or Fancybox for a feature-rich, highly customizable lightbox experience.
JavaScript image viewer.
Pros of Viewer.js
- More feature-rich, offering advanced image manipulation tools like zooming, rotating, and flipping
- Supports a wider range of image formats, including SVG
- Provides a more customizable UI with various display modes and toolbar options
Cons of Viewer.js
- Larger file size, which may impact page load times
- Steeper learning curve due to its more complex API and configuration options
- May be considered overkill for simple image gallery needs
Code Comparison
BaguetteBox.js (simple implementation):
baguetteBox.run('.gallery');
Viewer.js (basic setup):
const viewer = new Viewer(document.getElementById('images'), {
inline: true,
viewed() {
viewer.zoomTo(1);
},
});
BaguetteBox.js focuses on simplicity and ease of use, making it ideal for quick implementation of lightweight image galleries. Viewer.js, on the other hand, offers a more comprehensive set of features for advanced image viewing and manipulation, suitable for projects requiring extensive image interaction capabilities.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
baguetteBox.js
Simple and easy to use lightbox script written in pure JavaScript.
Table of contents
- Features
- Installation
- Importing
- Usage
- Customization
- API
- Responsive images
- Compatibility
- Contributing
- Donation
- Credits
- License
Features
- Written in pure JavaScript, no dependencies required
- Multiple-gallery support allows custom options for each
- Supports swipe gestures on touch-screen devices
- Full-screen mode available
- Modern and minimal look
- Image captions support
- Responsive images
- CSS3 transitions
- SVG buttons, no extra files to download
- Around 3.2KB gzipped
- With Accessibility in mind
Installation
You can use one of the following methods:
npm
npm install baguettebox.js --save
Yarn
yarn add baguettebox.js
Bower
bower install baguettebox.js --save
CDN
- Use one of the following CDN providers:
-
Copy URLs of the latest version (both
.js
and.css
files) -
Paste the URLs in your HTML file:
<link rel="stylesheet" href="<CSS URL>">
<script src="<JS URL>" async></script>
Manually
- Download
baguetteBox.min.css
andbaguetteBox.min.js
files from thedist
folder. - Include them somewhere in your document:
<link rel="stylesheet" href="css/baguetteBox.min.css">
<script src="js/baguetteBox.min.js" async></script>
Importing
Traditional approach
If you don't use JavaScript modules and include the file with a <script>
tag, you don't have to import anything explicitly. baguetteBox
will be available in the global scope.
CommonJS
const baguetteBox = require('baguettebox.js');
ES2015 modules
import baguetteBox from 'baguettebox.js';
Sass
@import 'baguettebox.js/dist/baguetteBox.min.css';
Usage
Initialize the script by running:
baguetteBox.run('.gallery');
where the first argument is a selector to a gallery (or galleries) containing a
tags. The HTML code may look like this:
<div class="gallery">
<a href="img/2-1.jpg" data-caption="Image caption">
<img src="img/thumbnails/2-1.jpg" alt="First image">
</a>
<a href="img/2-2.jpg">
<img src="img/thumbnails/2-2.jpg" alt="Second image">
</a>
...
</div>
To use captions put a title
or data-caption
attribute on the a
tag.
Note: if you import baguetteBox using the <script>
tag, remember to run it after the document has loaded:
<script>
window.addEventListener('load', function() {
baguetteBox.run('.gallery');
});
</script>
Customization
You can pass an object with custom options as the second parameter.
baguetteBox.run('.gallery', {
// Custom options
});
The following options are available:
Option | Type | Default | Description |
---|---|---|---|
captions | Boolean | function(element) | true | Display image captions. Passing a function will use a string returned by this callback. The only argument is a element containing the image. Invoked in the context of the current gallery array |
buttons | Boolean | 'auto' | 'auto' | Display buttons. 'auto' hides buttons on touch-enabled devices or when only one image is available |
leftArrow | rightArrow | closeX | string | default icons | This allows to customize buttons with own icons |
fullScreen | Boolean | false | Enable full screen mode |
noScrollbars | Boolean | false | Hide scrollbars when gallery is displayed |
bodyClass | String | 'baguetteBox-open' | Class name that will be appended to the body when lightbox is visible (works in IE 10+) |
ignoreClass | String | null | It will ignore images with given class put on a tag |
titleTag | Boolean | false | Use caption value also in the gallery img.title attribute |
async | Boolean | false | Load files asynchronously |
preload | Number | 2 | How many files should be preloaded |
animation | 'slideIn' | 'fadeIn' | false | 'slideIn' | Animation type |
afterShow | function | null | Callback to be run after showing the overlay |
afterHide | function | null | Callback to be run after hiding the overlay |
onChange | function(currentIndex, imagesCount) | null | Callback to be run when image changes |
overlayBackgroundColor | String | 'rgba (0,0,0,0.8)' | Background color for the lightbox overlay |
filter | RegExp | /.+\.(gif|jpe?g|png|webp)/i | Pattern to match image files. Applied to the a.href attribute |
API
run(selector, userOptions)
Initialize baguetteBox.js
- @param
selector
{string} - valid CSS selector used byquerySelectorAll
- @param
userOptions
{object} - custom options (see #Customization) - @return {array} - an array of gallery objects (reflects elements found by the selector)
show(index, gallery)
Show (if hidden) and move the gallery to a specific index
- @param
index
{number} - the position of the image - @param
gallery
{array} - gallery which should be opened, if omitted assumes the currently opened one - @return {boolean} - true on success or false if the index is invalid
Usage:
const gallery = baguetteBox.run('.gallery');
baguetteBox.show(index, gallery[0]);
showNext
Switch to the next image
- @return {boolean} - true on success or false if there are no more images to be loaded
showPrevious
Switch to the previous image
- @return {boolean} - true on success or false if there are no more images to be loaded
hide
Hide the gallery
destroy
Remove the plugin with any event bindings
Responsive images
To use this feature, simply put data-at-{width}
attributes on a
tags with a value being the path to the desired image. {width}
should be the maximum screen width the image can be displayed at. The script chooses the first image with {width}
greater than or equal to the current screen width for best user experience.
That last data-at-X
image is also used in the case of a screen larger than X.
Here's an example of what the HTML code can look like:
<a href="img/2-1.jpg"
data-at-450="img/thumbs/2-1.jpg"
data-at-800="img/small/2-1.jpg"
data-at-1366="img/medium/2-1.jpg"
data-at-1920="img/big/2-1.jpg">
<img src="img/thumbs/2-1.jpg">
</a>
If you have 1366x768 resolution baguetteBox.js will choose "img/medium/2-1.jpg"
. If, however, it's 1440x900 it'll choose "img/big/2-1.jpg"
. Keep the href
attribute as a fallback (link to a bigger image e.g. of HD size) for older browsers.
Compatibility
Desktop:
- IE 8+
- Chrome
- Firefox 3.6+
- Opera 12+
- Safari 5+
Mobile:
- Safari on iOS
- Chrome on Android
Contributing
Feel free to report any issues! If you wish to contribute by fixing a bug or implementing a new feature, please first read the CONTRIBUTING guide.
Donation
If you find this project useful and want to say thanks, you can buy me a cup of coffee :)
Credits
Creation of baguetteBox.js
was inspired by a great jQuery plugin touchTouch.
Huge thanks for providing a testing platform go to
License
Copyright (c) 2018 feimosi
This content is released under the MIT License.
Top Related Projects
JavaScript image gallery for mobile and desktop, modular, framework independent
A touchable jQuery lightbox
Full featured JavaScript image & video gallery. No dependencies
THE original Lightbox script (v2).
jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.
JavaScript image viewer.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot