Featherlight - ultra slim jQuery lightbox [![Build Status](https://travis-ci.org/noelboss/featherlight.svg?branch=master)](https://travis-ci.org/noelboss/featherlight)
------------------------

**Featherlight is a very lightweight jQuery lightbox plugin. For more information and demos, visit the [official website](http://noelboss.github.io/featherlight/).**

* Simple yet flexible
* Image, Ajax, iFrame and custom content support
* [Gallery Extension](https://github.com/noelboss/featherlight/#featherlight-gallery)
* Minimal CSS
* Name-spaced CSS and JavaScript
* Responsive
* Customizable via configuration object


## [» Download Current Release 0.4.10](https://github.com/noelboss/featherlight/archive/0.4.10.zip)

Here you'll find a [list of all the changes](https://github.com/noelboss/featherlight/blob/master/CHANGELOG.md) and you can also download [old releases](https://github.com/noelboss/featherlight/releases) or [the master including all the latest  bling](https://github.com/noelboss/featherlight/archive/master.zip).


# Installation

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/release/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/release/featherlight.min.js" type="text/javascript" charset="utf-8"></script>


# Usage

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.

## Bind Featherlight
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](#configuration).

***$content*** – jQuery Object or String: You can manually pass a jQuery object or a string (see [content filters](content_filters])) to be opened in the ligthbox.

## Manual calling of Featherlight
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 (see [content filters](content_filters])) to be opened in the ligthbox.

***configuration*** – Object: Object to configure certain aspects of the plugin. See [Configuration](#configuration).

# Configuration

Featherlight comes with a bunch of configuration-options which make it very flexible.
These options can be passed when calling `featherlight`.
Alternatively, they can specified as attribute on the elements triggering the lightbox;
for example, `<a data-featherlight-close-on-esc="false" ...>` has the same effect as
passing `{closeOnEsc: false}`.
You can also modify the `$.featherlight.defaults` directly which holds all the defaults:

	/* you can access and overwrite all defaults using $.featherlight.defaults */
	defaults: {
		autostart:    true,                   /* Initialize all links with that match "selector" on document ready */
		namespace:    'featherlight',         /* Name of the events and css class prefix */
		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 */
		variant:      null,                   /* Class that will be added to change look of the lightbox */
		resetCss:     false,                  /* Reset all css */
		background:   null,                   /* Custom DOM for the background, wrapper and the closebutton */
		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 */
		closeOnClick: 'background',           /* Close lightbox on click ('background', 'anywhere', or false) */
		closeOnEsc:   true,                   /* Close lightbox when pressing esc */
		closeIcon:    '&#10005;',             /* Close icon */
		beforeOpen:   $.noop,                 /* Called before open. can return false to prevent opening of lightbox. Gets event as parameter, this contains all data */
		beforeClose:  $.noop,                 /* Called before close. can return false to prevent opening of lightbox. Gets event as parameter, this contains all data */
		afterOpen:    $.noop,                 /* Called after open. Gets event as parameter, this contains all data */
		afterClose:   $.noop,                 /* Called after close. Gets event as parameter, this contains all data */
		contentFilters: ['jquery', 'image', 'html', 'ajax'], /* List of content filters to use to determine the content */
		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)();
		}
	}

================================================

	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.

================================================

	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.


================================================

	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

**Deprecated!** This will be changed in Release 0.4.0!

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.

================================================

	variant – String:  null
Pass your own CSS class to adjust the styling of the lightbox according to your need.


================================================

	resetCss – Boolean: false
Set this to true to remove all default css and start from designing scratch.

================================================


	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](http://api.jquery.com/animate/#duration). 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.)


================================================

	closeOnClick – 'background', 'anywhere' or false
If set, the close event is also bound to the either the background only or anywhere

================================================

	closeOnEsc – Boolean: true
If true, the lightbox is closed when pressing the ESC key

================================================

	closeIcon – String: &#10005`;
Oh the naming...

================================================

	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.


================================================

	beforeOpen, beforeClose – Function: null
Called before the open or close method is executed. This function can return false to prevent open or
close method from execution. `this` is an object and contains the triggering DOM element (if existing) and the related Featherlight objects.

	// example
	beforeOpen: function(event){
		console.log(this); // this contains all related elements
		returm false; // prevent lightbox from opening
	}


================================================

	afterOpen, afterClose – Function: null
Called after the open or close method is executed – it is not called, if the `before-` or `open` function returns `false`! It receives the event object. `this` is an object and contains the triggering DOM element (if existing) and the related Featherlight objects.

	// example
	afterOpen: function(event){
		console.log(this); // this contains all related elements
		alert(this.$content.hasClass('true')); // alert class of content
	}


================================================

	 open, close – Function

**Deprecated!** This functions will be removed in Release 0.4.0!


# Methods

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){
			// return false prevents the `afterOpen` function
		},

		/* closes the lightbox "this" contains $instance with the lightbox, and with the config */
		close: function(event){
			// return false prevents the `afterClose` function
		}
	}

You can overwrite a function like this:

	$.featherlight.methods.open = function() { alert('open!'); }

Check the source code for more details.

# Globals

`$.featherlight` has the following globals:

	current: function() /* returns the currently opened featherlight, or undefined */
	close:   function() /* closes the currently opened featherlight (if any) */

# Content Filters

There are many ways to specify content to featherlight. Featherlight uses a set of heuristics to determine the type, for example data ending with `.gif` will be assumed to be an image. The following are equivalent:

	<a href="#" data-featherlight="photo.gif">See in a lightbox</a>

	<a href="photo.gif" data-featherlight>See in a lightbox</a>

	<a id="#example" href="#">See in a lightbox</a>
	<script>$('#example').featherlight('photo.gif')</script>

In case the heuristic wouldn't work, you can specify which contentFiter to use:

	<a href="photo_without_extension" data-featherlight="image">See in a lightbox</a>

	<a id="force_as_image" href="photo_without_extension">See in a lightbox</a>
	<script>
		$('#force_as_image').featherlight('image');
		// Equivalent:
		$('#force_as_image').featherlight({type: {image: true}});
	</script>

	<a id="force_as_image2" href="#">See in a lightbox</a>
	<script>$('#force_as_image2').featherlight('photo_without_extension', {type:{image: true}});</script>

You can add your own heuristics, for example:

	$.featherlight.contentFilters.feed = {
		regex: /^feed:/
		process: function(url) { /* deal with url */ return $('Loading...'); }
	}
	$.featherlight.defaults.contentFilters.unshift('feed');

This way the following would be possible:

	<a href="feed://some_url" data-featherlight>See the feed in a lightbox</a>

**Deprecated!** `type` will be changed in Release 0.4.0!

# Examples

## Use link-hashtags to open lightbox
	<a href="#targetElement" class="fl">Open</a>
	<div id="targetElement">Lightbox</div>

	$('a.fl').featherlight({
		targetAttr: 'href'
	});

## Open images with Featherlight
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>

## Open lightbox with ajax content
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>


# IE8 background transparency
If you want the background in IE8 to be translucent, use data:image before the rgba background:

	background: url(data:image/png;base64,[encoded data, 4920 characters]);
	background: rgba(0, 0, 0, 0.8);

---

## Featherlight Gallery
Featherlight was created to be as small and simple as possible. Therefore it does not provide all functionality imaginable. But, since its small and simple, it can be extended easily. featherlight.gallery.js is a small extension that turns your set of links into a [gallery](http://noelboss.github.io/featherlight/gallery.html).

	$('a.gallery').featherlightGallery({
		openSpeed: 300
	});


### Gallery configuration

The gallery also has a range of configuration options and the following defaults:

	gallery: {
		previous: '&#9664;',   /* Code that is used as previews icon */
		next: '&#9654;',       /* Code that is used as next icon */
		fadeIn: 100,           /* fadeIn speed when image is loaded */
		fadeOut: 300           /* fadeOut speed before image is loaded */
	}

Example:

	$('a.gallery').featherlightGallery({
		gallery: {
			previous: '«',
			next: '»',
			fadeIn: 300
		},
		openSpeed: 300
	});

The gallery responds to custom events `previous` and `next` to navigate to the previous and next images.

Instead of navigation buttons it will use swipe events on touch devices, assuming that one of the [supported swipe libraries](https://github.com/noelboss/featherlight/wiki/Gallery:-swipe-on-touch-devices) is also installed.

### Gallery installation

Simply include the extension CSS and JavaScript Files after the regular featherlight files like this:

	<link href="//rawgithub.com/noelboss/featherlight/master/release/featherlight.min.css" type="text/css" rel="stylesheet" title="Featherlight Styles" />
	<link href="//rawgithub.com/noelboss/featherlight/master/release/featherlight.gallery.min.css" type="text/css" rel="stylesheet" title="Featherlight Gallery Styles" />

Add the JavaScript at the bottom of the body:

	<script src="//code.jquery.com/jquery-latest.js"></script>
	<script src="//rawgithub.com/noelboss/featherlight/master/release/featherlight.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="//rawgithub.com/noelboss/featherlight/master/release/featherlight.gallery.min.js" type="text/javascript" charset="utf-8"></script>

Check out the example here: [Gallery with Featherlight](http://noelboss.github.io/featherlight/gallery.html)
