Featherlight is a very lightweight jQuery lightbox plugin. For more information and demos, visit the official website.
- simple yet flexible
- image support
- ajax support
- iframe support
- minimal css
- name-spaced
- responsive
- completely customizable via configuration object
- call with custom content
All styling is done using CSS so you'll want to include the Featherlight CSS in your head.
<link href="//rawgithub.com/noelboss/featherlight/master/src/featherlight.min.css" type="text/css" rel="stylesheet" title="Featherlight Styles" />
Be aware that Featherlight uses very unspecific CSS selectors to help you overwrite every aspect. This means in turn, that if you're not following a modularized approach to write CSS (which you should! It's terrific!) and have many global and specific definitions (read ID's and such – which you shouldn't), these definitions can break the Featherlight styling.
Featherlight requires jQuery version 1.7.0 or higher. It's recommended to include the javascript at the bottom of the page before the closing </body> tag.
<script src="//code.jquery.com/jquery-latest.js"></script>
<script src="//rawgithub.com/noelboss/featherlight/master/src/featherlight.min.js" type="text/javascript" charset="utf-8"></script>
By default, featherlight acts on all elements using the 'data-featherlight' attribute. An element with this attribute triggers the lightbox. The value of the attribute acts as selector for an element that's opened as lightbox.
<a href="#" data-featherlight="#mylightbox">Open element in lightbox</a>
<div id="mylightbox">This div will be opened in a lightbox</div>
Featherlight is smart. 'data-featherlight' can also contain a link to an image, an ajax-url or even DOM code:
<span data-featherlight="myimage.png">Open image in lightbox</a>
<span data-featherlight="myhtml.html .selector">Open ajax content in lightbox</a>
<span data-featherlight="<p>Fancy DOM Lightbox!</p>">Open some DOM in lightbox</span>
it also works with links using href and the "image" and "ajax" keywords (this can also be manually set with the configuration options type.image and type.ajax):
<a href="myimage.png" data-featherlight="image">Open image in lightbox</a>
<a href="myhtml.html .selector" data-featherlight="ajax">Open ajax content in lightbox</a>
By default, Featherlight initializes all elements matching defaults.selector on document ready. If you want to prevent this, set $.featherlight.defaults.autostart to false before the DOM is ready.
You can bind the Featherlight events on any element using the following code:
$('.myElement').featherlight(configuration, $content);
It will then look for the targetAttr (by default "data-featherlight") on this element and use its value to find the content that will be opened as lightbox when you click on the element.
configuration – Object: Object to configure certain aspects of the plugin. See Configuration.
$content – jQuery Object or String: You can manually pass a jQuery object or a string containing HTML Code to be opened in the ligthbox.
In cases where you don't want an Element to act as Trigger you can call Featherlight manually. You can use this for example in an ajax callback to display the response data.
$.featherlight($content, configuration);
$content – jQuery Object or String: You can manually pass a jQuery object or a string containing HTML Code to be opened in the ligthbox.
configuration – Object: Object to configure certain aspects of the plugin. See Configuration.
Featherlight comes with a bunch of configuration-options which make it very flexible. Pass your options in an object to the function call or overwrite $.featherlight.defaults. This is the whole default object:
/* you can access and overwrite all defaults using $.fl.defaults */
defaults: {
selector: '[data-featherlight]', /* elements that trigger the lightbox */
context: 'body', /* context used to search for the lightbox content and triggers */
type: { /* manually set type of lightbox. Otherwise, it will check for the targetAttrs value. */
image: false,
ajax: false
},
targetAttr: 'data-featherlight', /* attribute of the triggered element that contains the selector to the lightbox content */
openTrigger: 'click', /* event that triggers the lightbox */
closeTrigger: 'click', /* event that triggers the closing of the lightbox */
openSpeed: 250, /* duration of opening animation */
closeSpeed: 250, /* duration of closing animation */
namespace: 'featherlight', /* name of the events and css class prefix */
resetCss: false, /* reset all css */
variant: null, /* class that will be added to change look of the lightbox */
closeOnBg: true, /* close lightbox on click on the background */
closeOnEsc: true, /* close lightbox when pressing esc */
background: null, /* custom DOM for the background, wrapper and the closebutton */
autostart: true, /* initialize all links with that match "selector" on document ready */
open: function(event){ /* opens the lightbox "this" contains $instance with the lightbox, and with the config */
$.proxy($.featherlight.methods.open, this, event)();
},
close: function(event){ /* closes the lightbox "this" contains $instance with the lightbox, and with the config */
$.proxy($.featherlight.methods.close, this, event)();
}
}
================================================
selector – String: '[data-featherlight]'
Selector used to collect triggering elements when document is ready.
================================================
context – String: 'body'
Context used for selecting elements matching "selector". Useful of you only want to bind featherbox to parts of the DOM, for example in content loaded via ajax.
================================================
type – Object: image: false, ajax: false
The type object allows you to manually set what type the lightbox is. Set the value of type.image or type.ajax to true. Otherwise, the value from targetAttr will be to determine the type of the lightbox. Example:
$('.image-lightbox', $('.gallery')).featherlight({
type: {
image: true
}
});
================================================
targetAttr – String: 'data-featherlight'
Attribute on the triggering element pointing to the target element that will be opened in the lightbox.
================================================
openTrigger & closeTrigger – String: 'click'
Events that are used to open or close the lightbox. The close event is bound to the close button and to the lightbox background (if enabled)
================================================
openSpeed & closeSpeed – Integer or String: 250
Defines the speed for the opening and close animations. Values allowed are jQuery animation durations. Avoid the usage of 0 since this would cause a reversal of time and the end of the world! (Okay, kidding, just the closing callback would not be fired and unused ghost-DOM would linger around, but still.)
================================================
namespace – String: 'featherlight'
All functions bound to elements are namespaced. This is also used to prefix all CSS classes for the background, the content-wrapper and the close button.
================================================
resetCss – Boolean: false
Set this to true to remove all default css and start from designing scratch.
================================================
variant – String: null
Pass your own CSS class to adjust the styling of the lightbox according to your need. You can also use the data-featherlight-variant attribute on the element triggering the lightbox.
================================================
closeOnBg – Boolean: true
If true, the close event is also bound to the background
================================================
closeOnEsc – Boolean: true
If true, the lightbox is closed when pressing the ESC key
================================================
background – DOM String: null
You can provide the wrapping DOM. This is a bit tricky and just for the advanced users. It's recommended to study the plugin code. But you need to provide an element with a "{namespace}-close" class: the content of the lightbox will be added after this element.
================================================
autostart – Boolean: true
By default, Featherlight finds all elements that match "selector" and binds the open and close functions. To disable, set $.featherlight.defaults.autostart = false; before the document ready event is fired.
================================================
open – Function
This is the open function used to open the lightbox. It receives the event object. "this" is an object and contains the triggering DOM element (if existing) and the related Featherlight objects: $fl – Containing the whole lightbox: background, content wrapper, close button and content. $content – The content thats wrapped with the background and prepended with the close button.
open: function(e){
$.proxy($.featherlight.methods.open, this, e)();
}
================================================
close – Function
This is the close function used to close the lightbox. It receives the event object. "this" is an object and contains the triggering DOM element (if existing) and the related Featherlight objects: $fl – Containing the whole lightbox: background, content wrapper, close button and content. $content – The content thats wrapped with the background and prepended with the close button.
close: function(e){
$.proxy($.featherlight.methods.close, this, e)();
}
All of Featherlight's methods are stored in $.featherlight.methods and can therefore be overwritten like the configuration defaults.
methods: { /* you can access and overwrite all methods using */
/* setup iterates over a single instance of featherlight and prepares the background and binds the events */
setup: function(config, content){
// ...
},
/* this method prepares the content and converts it into a jQuery object */
getContent: function(){
// ...
},
/* opens the lightbox "this" contains $instance with the lightbox, and with the config */
open: function(event){
// ...
},
/* closes the lightbox "this" contains $instance with the lightbox, and with the config */
close: function(event){
// ...
}
}
You can overwrite a function like this:
$.featherlight.methods.open = function() { alert('open!'); }
<a href="#targetElement" class="fl">Open</a>
<div id="targetElement">Lightbox</div>
$('a.fl').featherlight({
targetAttr: 'href'
});
Us a link and point the data-featherlight attribute to the desired attribute which contains the link...
<a href="myimage.jpg" data-featherlight="image">Open Image</a>
...or directly provide the link as the data-featherlight attribute:
<span data-featherlight="myimage.jpg">Open Image</span>
Use Featherlight with ajax using 'ajax' keyword or providing a url. It even supports selecting elements inside the response document.
<a href="url.html .jQuery-Selector" data-featherlight="ajax">Open Ajax Content</a>
or you can provide the link directly as the featherlight-attribute:
<a href="url.html .jQuery-Selector" data-featherlight="ajax">Open Ajax Content</a>
Add class to overwrite styling. For a full example check out the index.html
$('a.fl').featherlight({
variant: 'myCssClass'
});
You can also use a data attribute:
<a href="#" data-featherlight="#myLightbox" data-featherlight-variant="blingbling">Special Lightbox</a>
If you want the background in IE8 to be translucent, use data:image before the rgba background:
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAKQWlDQ1BJQ0MgUHJvZmlsZQAASA2dlndUU9kWh8+9N73QEiIgJfQaegkg0jtIFQRRiUmAUAKGhCZ2RAVGFBEpVmRUwAFHhyJjRRQLg4Ji1wnyEFDGwVFEReXdjGsJ7601896a/cdZ39nnt9fZZ+9917oAUPyCBMJ0WAGANKFYFO7rwVwSE8vE9wIYEAEOWAHA4WZmBEf4RALU/L09mZmoSMaz9u4ugGS72yy/UCZz1v9/kSI3QyQGAApF1TY8fiYX5QKUU7PFGTL/BMr0lSkyhjEyFqEJoqwi48SvbPan5iu7yZiXJuShGlnOGbw0noy7UN6aJeGjjAShXJgl4Gej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