Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including lity with all npm packages installed. Try it out:

require("jquery/package.json"); // jquery is a peer dependency. var lity = require("lity")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

lity v2.3.1

Lightweight, accessible and responsive lightbox.


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.

It requires jQuery or Zepto (with the callbacks, deferred and data modules).


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="vendor/jquery.js"></script>
<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="" data-lity>Image</a>
<a href="#inline" data-lity>Inline</a>
<a href="" data-lity>iFrame Youtube</a>
<a href="" data-lity>iFrame Vimeo</a>
<a href=",+Mountain+View,+CA" data-lity>Google Maps</a>

<div id="inline" style="background:#fff" class="lity-hide">
    Inline content

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).

Return value

A Lity instance.


// Open a URL or HTML in a lightbox
lity('<p>Some content to show...</p>');

// Bind as an event handler
$(document).on('click', '[data-my-lightbox]', lity);

The Lity instance

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('');



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.


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.

Available events


Triggered before the lightbox is opened.

$(document).on('lity:open', function(event, instance) {
    console.log('Lightbox opened');


Triggered when the lightbox is ready.

$(document).on('lity:ready', function(event, instance) {
    console.log('Lightbox ready');


Triggered before the lightbox is closed.

$(document).on('lity:close', function(event, instance) {
    console.log('Lightbox closed');


Triggered when the closing animation is finished and just before the lightbox is removed from the DOM.

$(document).on('lity:remove', function(event, instance) {
    console.log('Lightbox removed');


Triggered when the instance is resized, usually when the user resizes the window.

$(document).on('lity:resize', function(event, instance) {
    console.log('Lightbox resized');


Copyright (c) 2015-2017 Jan Sorgalla. Released under the MIT license.


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free