Lity is a ultra-lightweight, accessible and responsive lightbox plugin which supports images, iframes and inline content out of the box.
Minified and gzipped, its total footprint weights about 3kB.
This version does not require jQuery anymore.
- TIFF files are not supported anymore (did we ever use them?). However, AVIF support is added instead.
- IE support is gone as more current ECMAScript is being used.
- Grunt is still there but updated (as are all of its plugins). And, it does not remove the CSS
:root
stuff anymore. - Inline
max-height
styling is not set anymore. If you need this there are (better) CSS only ways to achieve this.
All ready-to-use files are located in the dist/
directory.
Include the Lity javascript and css files and its dependencies in your HTML document:
<link href="dist/lity.css" rel="stylesheet">
<script src="dist/lity.js"></script>
Lity can also be installed via Bower or npm.
Add the data-lity
attribute to <a>
elements for which you want the links to
be opened in a lightbox:
<a href="https://farm9.staticflickr.com/8642/16455005578_0fdfc6c3da_b.jpg" data-lity>Image</a>
<a href="#inline" data-lity>Inline</a>
<a href="https://www.youtube.com/watch?v=XSGBVzeBUbk" data-lity>iFrame Youtube</a>
<a href="https://vimeo.com/1084537" data-lity>iFrame Vimeo</a>
<a href="https://maps.google.com/maps?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA" data-lity>Google Maps</a>
<div id="inline" style="background:#fff" class="lity-hide">
Inline content
</div>
If you want to open another URI than defined in the href
attribute, just add
a data-lity-target
with the URI:
<a href="/image.html" data-lity data-lity-target="/image-preview.jpg">Image</a>
The lity
function can be either used directly to open URLs (or HTML) in a
lightbox or as an event handler.
Lity lity(string target, [Object options, [, HTMLElement|$ opener]])
target
: The URL or HTML to open.options
: Options as an object of key-value pairs.opener
: The element which triggered opening the lightbox (if used as a event handler, this is automatically set to the element which triggered the event).
A Lity
instance.
// Open a URL or HTML in a lightbox
lity('https://www.youtube.com/watch?v=XSGBVzeBUbk');
lity('<p>Some content to show...</p>');
// Bind as an event handler (vanilla version of jQuery’s .on() delegation)
document.addEventListener('click', function(e) {
for (var target = e.target; target && target != this; target = target.parentNode) {
if (target.matches('[data-lightbox]')) {
lity.call(target, e);
break;
}
}
}, false);
If you open a lightbox programmatically, the lity
function returns a Lity
instance you can use to interact with the lightbox.
The Lity
instance is also passed as the second argument to the
event handlers.
var instance = lity('https://www.youtube.com/watch?v=XSGBVzeBUbk');
Closes the lightbox and returns a promise which resolves once the closing animation is finished.
instance.close().then(function() {
console.log('Lightbox closed');
});
Returns the root HTML element.
var element = instance.element();
Returns the HTML element which triggered opening the lightbox.
var opener = instance.opener();
Note: The value might be undefined if the lightbox has been opened programmatically and not by a click event handler and no opener argument was provided.
Returns the content HTML element.
var content = instance.content();
Note: The value is undefined while the content is loading.
Sets or returns options of the instance.
var allOptions = instance.options();
var template = instance.options('template');
instance.options('template', '<div>...</div>');
var closeOnEsc = instance.options('esc');
instance.options('esc', false);
All events receive the Lity
instance as the second
argument.
Triggered before the lightbox is opened.
document.addEventListener('lity:open', function(event, instance) {
console.log('Lightbox opened');
}, false);
Triggered when the lightbox is ready.
document.addEventListener('lity:ready', function(event, instance) {
console.log('Lightbox ready');
}, false);
Triggered before the lightbox is closed.
document.addEventListener('lity:close', function(event, instance) {
console.log('Lightbox closed');
}, false);
Triggered when the closing animation is finished and just before the lightbox is removed from the DOM.
document.addEventListener('lity:remove', function(event, instance) {
console.log('Lightbox removed');
}, false);
Triggered when the instance is resized, usually when the user resizes the window.
document.addEventListener('lity:resize', function(event, instance) {
console.log('Lightbox resized');
}, false);
Copyright (c) 2015-2022 Jan Sorgalla, Anton Andreasson. Released under the MIT license.