Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

feimosi/baguetteBox.js

GitHub ReleaseMIT LicensenpmBuild StatusDonateTwitterTwitter 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:

<linkrel="stylesheet"href="<CSS URL>"><scriptsrc="<JS URL>"async></script>

Manually

  1. DownloadbaguetteBox.min.css andbaguetteBox.min.js files from thedist folder.
  2. Include them somewhere in your document:
<linkrel="stylesheet"href="css/baguetteBox.min.css"><scriptsrc="js/baguetteBox.min.js"async></script>

Composer

Add the following to yourcomposer.json file (updating the version as required) and then runcomposer install orcomposer update:

{"repositories": [        {"type":"vcs","url":"https://github.com/feimosi/baguetteBox.js"        }    ],"require": {"feimosi/baguettebox.js":"dev-main"    }}

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

constbaguetteBox=require('baguettebox.js');

ES2015 modules

importbaguetteBoxfrom'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) containinga tags. The HTML code may look like this:

<divclass="gallery"><ahref="img/2-1.jpg"data-caption="Image caption"><imgsrc="img/thumbnails/2-1.jpg"alt="First image"></a><ahref="img/2-2.jpg"><imgsrc="img/thumbnails/2-2.jpg"alt="Second image"></a>    ...</div>

To use captions put atitle ordata-caption attribute on thea 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:

OptionTypeDefaultDescription
captionsBoolean |function(element)trueDisplay image captions. Passing a function will use a string returned by this callback. The only argument isa element containing the image. Invoked in the context of the current gallery array
buttonsBoolean |'auto''auto'Display buttons.'auto' hides buttons on touch-enabled devices or when only one image is available
leftArrow |rightArrow |closeXstringdefault iconsThis allows to customize buttons with own icons
fullScreenBooleanfalseEnable full screen mode
noScrollbarsBooleanfalseHide scrollbars when gallery is displayed
bodyClassString'baguetteBox-open'Class name that will be appended to thebody when lightbox is visible (works in IE 10+)
ignoreClassStringnullIt will ignore images with given class put ona tag
titleTagBooleanfalseUse caption value also in the galleryimg.title attribute
asyncBooleanfalseLoad files asynchronously
preloadNumber2How many files should be preloaded
animation'slideIn' |'fadeIn' |false'slideIn'Animation type
afterShowfunctionnullCallback to be run after showing the overlay
afterHidefunctionnullCallback to be run after hiding the overlay
onChangefunction(currentIndex, imagesCount)nullCallback to be run when image changes
overlayBackgroundColorString'rgba
(0,0,0,0.8)'
Background color for the lightbox overlay
filterRegExp/.+\.(gif|jpe?g|png|webp)/iPattern to match image files. Applied to thea.href attribute

API

run(selector, userOptions)

Initialize baguetteBox.js

  • @paramselector {string} - valid CSS selector used byquerySelectorAll
  • @paramuserOptions {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

  • @paramindex {number} - the position of the image
  • @paramgallery {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:

constgallery=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 putdata-at-{width} attributes ona 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 lastdata-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:

<ahref="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"><imgsrc="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 thehref 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 theCONTRIBUTING guide.

Donation

If you find this project useful and want to say thanks, you can buy me a cup of coffee :)

Credits

Creation ofbaguetteBox.js was inspired by a great jQuery plugintouchTouch.

Huge thanks for providing a testing platform go toBrowserStack

License

Copyright (c) 2018feimosi

This content is released under theMIT License.


[8]ページ先頭

©2009-2025 Movatter.jp