All Projects → feimosi → Baguettebox.js

feimosi / Baguettebox.js

Licence: mit
⚡ Simple and easy to use lightbox script written in pure JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to Baguettebox.js

fancybox
jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.
Stars: ✭ 7,261 (+222.42%)
Mutual labels:  gallery, responsive, modal, lightbox
react-gallery-carousel
Mobile-friendly gallery carousel 🎠 with server side rendering, lazy loading, fullscreen, thumbnails, touch, mouse emulation, RTL, keyboard navigation and customisations.
Stars: ✭ 178 (-92.1%)
Mutual labels:  gallery, responsive, modal, lightbox
Glightbox
Pure Javascript lightbox with mobile support. It can handle images, videos with autoplay, inline content and iframes
Stars: ✭ 702 (-68.83%)
Mutual labels:  gallery, lightbox, responsive
Lity
Lightweight, accessible and responsive lightbox.
Stars: ✭ 1,051 (-53.33%)
Mutual labels:  modal, lightbox, responsive
Natural Gallery Js
A lazy load, infinite scroll and natural layout list gallery
Stars: ✭ 93 (-95.87%)
Mutual labels:  gallery, lightbox, responsive
Photoviewer
🌀 A JS plugin to view images just like in Windows.
Stars: ✭ 203 (-90.99%)
Mutual labels:  modal, lightbox, responsive
hes-gallery
Light, dependency free, responsive gallery script
Stars: ✭ 27 (-98.8%)
Mutual labels:  gallery, responsive, lightbox
Ngx Gallery
Angular Gallery, Carousel and Lightbox
Stars: ✭ 417 (-81.48%)
Mutual labels:  gallery, modal, lightbox
Magnify
🖼 A jQuery plugin to view images just like in Windows. Browser support IE7+!
Stars: ✭ 177 (-92.14%)
Mutual labels:  modal, lightbox, responsive
Spotlight
Web's most easy to integrate lightbox gallery library. Super-lightweight, outstanding performance, no dependencies.
Stars: ✭ 799 (-64.52%)
Mutual labels:  gallery, lightbox
Halkabox.js
A simple and basic Javascript lightbox.
Stars: ✭ 75 (-96.67%)
Mutual labels:  lightbox, vanilla-javascript
Jbox
jBox is a jQuery plugin that makes it easy to create customizable tooltips, modal windows, image galleries and more.
Stars: ✭ 1,251 (-44.45%)
Mutual labels:  modal, lightbox
Simplelightbox
Touch-friendly image lightbox for mobile and desktop
Stars: ✭ 744 (-66.96%)
Mutual labels:  modal, lightbox
React Poppop
A mobile support and multi-directional modal for ReactJS
Stars: ✭ 78 (-96.54%)
Mutual labels:  modal, responsive
Django Admin Interface
django's default admin interface made customizable. popup windows replaced by modals. :mage: ⚡️
Stars: ✭ 717 (-68.16%)
Mutual labels:  modal, responsive
Particles.js
A lightweight, dependency-free and responsive javascript plugin for particle backgrounds.
Stars: ✭ 1,336 (-40.67%)
Mutual labels:  responsive, vanilla-javascript
React Modal Image
A tiny React component providing modal image Lightbox.
Stars: ✭ 97 (-95.69%)
Mutual labels:  modal, lightbox
Bootstrap4 Fs Modal
A simple way to improve UX of Bootstrap 4 modals on mobile phones.
Stars: ✭ 102 (-95.47%)
Mutual labels:  modal, responsive
Hugo Theme Console
A minimal, responsive and light theme for Hugo inspired by Linux console.
Stars: ✭ 143 (-93.65%)
Mutual labels:  gallery, responsive
Vue Pure Lightbox
Very simple lightbox plugin (without any dependencies) for Vuejs 🌅
Stars: ✭ 142 (-93.69%)
Mutual labels:  gallery, lightbox

baguetteBox.js

GitHub Release MIT License dependency Status devDependency Status npm Build Status

Donate Twitter Twitter Follow

Simple and easy to use lightbox script written in pure JavaScript.

Demo page

Demo Page screenshot

Table of contents

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

  1. Use one of the following CDN providers:
  1. Copy URLs of the latest version (both .js and .css files)

  2. Paste the URLs in your HTML file:

<link rel="stylesheet" href="<CSS URL>">
<script src="<JS URL>" async></script>

Manually

  1. Download baguetteBox.min.css and baguetteBox.min.js files from the dist folder.
  2. 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
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 by querySelectorAll
  • @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 BrowserStack

License

Copyright (c) 2018 feimosi

This content is released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].