3 * by Lokesh Dhakar - http://lokeshdhakar.com/projects/lightbox2/
5 * @license http://creativecommons.org/licenses/by/2.5/
6 * - Free for use in both personal and commercial projects
7 * - Attribution requires leaving author name, author link, and the license info intact
14 var LightboxOptions = (function() {
15 function LightboxOptions() {
16 this.fadeDuration = 500;
17 this.fitImagesInViewport = true;
18 this.resizeDuration = 700;
19 this.positionFromTop = 50;
20 this.showImageNumberLabel = true;
21 this.alwaysShowNavOnTouchDevices = false;
22 this.wrapAround = false;
25 // Change to localize to non-english language
26 LightboxOptions.prototype.albumLabel = function(curImageNum, albumSize) {
27 return "Image " + curImageNum + " of " + albumSize;
30 return LightboxOptions;
34 var Lightbox = (function() {
35 function Lightbox(options) {
36 this.options = options;
38 this.currentImageIndex = void 0;
42 Lightbox.prototype.init = function() {
47 // Loop through anchors and areamaps looking for either data-lightbox attributes or rel attributes
48 // that contain 'lightbox'. When these are clicked, start lightbox.
49 Lightbox.prototype.enable = function() {
51 $('body').on('click', 'a[rel^=lightbox], area[rel^=lightbox], a[data-lightbox], area[data-lightbox]', function(event) {
52 self.start($(event.currentTarget));
57 // Build html for the lightbox and the overlay.
58 // Attach event handlers to the new DOM elements. click click click
59 Lightbox.prototype.build = function() {
61 $("<div id='lightboxOverlay' class='lightboxOverlay'></div><div id='lightbox' class='lightbox'><div class='lb-outerContainer'><div class='lb-container'><img class='lb-image' src='' /><div class='lb-nav'><a class='lb-prev' href='' ></a><a class='lb-next' href='' ></a></div><div class='lb-loader'><a class='lb-cancel'></a></div></div></div><div class='lb-dataContainer'><div class='lb-data'><div class='lb-details'><span class='lb-caption'></span><span class='lb-number'></span></div><div class='lb-closeContainer'><a class='lb-close'></a></div></div></div></div>").appendTo($('body'));
63 // Cache jQuery objects
64 this.$lightbox = $('#lightbox');
65 this.$overlay = $('#lightboxOverlay');
66 this.$outerContainer = this.$lightbox.find('.lb-outerContainer');
67 this.$container = this.$lightbox.find('.lb-container');
69 // Store css values for future lookup
70 this.containerTopPadding = parseInt(this.$container.css('padding-top'), 10);
71 this.containerRightPadding = parseInt(this.$container.css('padding-right'), 10);
72 this.containerBottomPadding = parseInt(this.$container.css('padding-bottom'), 10);
73 this.containerLeftPadding = parseInt(this.$container.css('padding-left'), 10);
75 // Attach event handlers to the newly minted DOM elements
76 this.$overlay.hide().on('click', function() {
81 this.$lightbox.hide().on('click', function(event) {
82 if ($(event.target).attr('id') === 'lightbox') {
88 this.$outerContainer.on('click', function(event) {
89 if ($(event.target).attr('id') === 'lightbox') {
95 this.$lightbox.find('.lb-prev').on('click', function() {
96 if (self.currentImageIndex === 0) {
97 self.changeImage(self.album.length - 1);
99 self.changeImage(self.currentImageIndex - 1);
104 this.$lightbox.find('.lb-next').on('click', function() {
105 if (self.currentImageIndex === self.album.length - 1) {
108 self.changeImage(self.currentImageIndex + 1);
113 this.$lightbox.find('.lb-loader, .lb-close').on('click', function() {
119 // Show overlay and lightbox. If the image is part of a set, add siblings to album array.
120 Lightbox.prototype.start = function($link) {
122 var $window = $(window);
124 $window.on('resize', $.proxy(this.sizeOverlay, this));
126 $('select, object, embed').css({
135 function addToAlbum($link) {
137 link: $link.attr('href'),
138 title: $link.attr('data-title') || $link.attr('title')
142 // Support both data-lightbox attribute and rel attribute implementations
143 var dataLightboxValue = $link.attr('data-lightbox');
146 if (dataLightboxValue) {
147 $links = $($link.prop("tagName") + '[data-lightbox="' + dataLightboxValue + '"]');
148 for (var i = 0; i < $links.length; i = ++i) {
149 addToAlbum($($links[i]));
150 if ($links[i] === $link[0]) {
155 if ($link.attr('rel') === 'lightbox') {
156 // If image is not part of a set
159 // If image is part of a set
160 $links = $($link.prop("tagName") + '[rel="' + $link.attr('rel') + '"]');
161 for (var j = 0; j < $links.length; j = ++j) {
162 addToAlbum($($links[j]));
163 if ($links[j] === $link[0]) {
171 var top = $window.scrollTop() + this.options.positionFromTop;
172 var left = $window.scrollLeft();
176 }).fadeIn(this.options.fadeDuration);
178 this.changeImage(imageNumber);
181 // Hide most UI elements in preparation for the animated resizing of the lightbox.
182 Lightbox.prototype.changeImage = function(imageNumber) {
185 this.disableKeyboardNav();
186 var $image = this.$lightbox.find('.lb-image');
188 this.$overlay.fadeIn(this.options.fadeDuration);
190 $('.lb-loader').fadeIn('slow');
191 this.$lightbox.find('.lb-image, .lb-nav, .lb-prev, .lb-next, .lb-dataContainer, .lb-numbers, .lb-caption').hide();
193 this.$outerContainer.addClass('animating');
195 // When image to show is preloaded, we send the width and height to sizeContainer()
196 var preloader = new Image();
197 preloader.onload = function() {
198 var $preloader, imageHeight, imageWidth, maxImageHeight, maxImageWidth, windowHeight, windowWidth;
199 $image.attr('src', self.album[imageNumber].link);
201 $preloader = $(preloader);
203 $image.width(preloader.width);
204 $image.height(preloader.height);
206 if (self.options.fitImagesInViewport) {
207 // Fit image inside the viewport.
208 // Take into account the border around the image and an additional 10px gutter on each side.
210 windowWidth = $(window).width();
211 windowHeight = $(window).height();
212 maxImageWidth = windowWidth - self.containerLeftPadding - self.containerRightPadding - 20;
213 maxImageHeight = windowHeight - self.containerTopPadding - self.containerBottomPadding - 120;
215 // Is there a fitting issue?
216 if ((preloader.width > maxImageWidth) || (preloader.height > maxImageHeight)) {
217 if ((preloader.width / maxImageWidth) > (preloader.height / maxImageHeight)) {
218 imageWidth = maxImageWidth;
219 imageHeight = parseInt(preloader.height / (preloader.width / imageWidth), 10);
220 $image.width(imageWidth);
221 $image.height(imageHeight);
223 imageHeight = maxImageHeight;
224 imageWidth = parseInt(preloader.width / (preloader.height / imageHeight), 10);
225 $image.width(imageWidth);
226 $image.height(imageHeight);
230 self.sizeContainer($image.width(), $image.height());
233 preloader.src = this.album[imageNumber].link;
234 this.currentImageIndex = imageNumber;
237 // Stretch overlay to fit the viewport
238 Lightbox.prototype.sizeOverlay = function() {
240 .width($(window).width())
241 .height($(document).height());
244 // Animate the size of the lightbox to fit the image we are showing
245 Lightbox.prototype.sizeContainer = function(imageWidth, imageHeight) {
248 var oldWidth = this.$outerContainer.outerWidth();
249 var oldHeight = this.$outerContainer.outerHeight();
250 var newWidth = imageWidth + this.containerLeftPadding + this.containerRightPadding;
251 var newHeight = imageHeight + this.containerTopPadding + this.containerBottomPadding;
253 function postResize() {
254 self.$lightbox.find('.lb-dataContainer').width(newWidth);
255 self.$lightbox.find('.lb-prevLink').height(newHeight);
256 self.$lightbox.find('.lb-nextLink').height(newHeight);
260 if (oldWidth !== newWidth || oldHeight !== newHeight) {
261 this.$outerContainer.animate({
264 }, this.options.resizeDuration, 'swing', function() {
272 // Display the image and it's details and begin preload neighboring images.
273 Lightbox.prototype.showImage = function() {
274 this.$lightbox.find('.lb-loader').hide();
275 this.$lightbox.find('.lb-image').fadeIn('slow');
278 this.updateDetails();
279 this.preloadNeighboringImages();
280 this.enableKeyboardNav();
283 // Display previous and next navigation if appropriate.
284 Lightbox.prototype.updateNav = function() {
285 // Check to see if the browser supports touch events. If so, we take the conservative approach
286 // and assume that mouse hover events are not supported and always show prev/next navigation
287 // arrows in image sets.
288 var alwaysShowNav = false;
290 document.createEvent("TouchEvent");
291 alwaysShowNav = (this.options.alwaysShowNavOnTouchDevices)? true: false;
294 this.$lightbox.find('.lb-nav').show();
296 if (this.album.length > 1) {
297 if (this.options.wrapAround) {
299 this.$lightbox.find('.lb-prev, .lb-next').css('opacity', '1');
301 this.$lightbox.find('.lb-prev, .lb-next').show();
303 if (this.currentImageIndex > 0) {
304 this.$lightbox.find('.lb-prev').show();
306 this.$lightbox.find('.lb-prev').css('opacity', '1');
309 if (this.currentImageIndex < this.album.length - 1) {
310 this.$lightbox.find('.lb-next').show();
312 this.$lightbox.find('.lb-next').css('opacity', '1');
319 // Display caption, image number, and closing button.
320 Lightbox.prototype.updateDetails = function() {
323 // Enable anchor clicks in the injected caption html.
324 // Thanks Nate Wright for the fix. @https://github.com/NateWr
325 if (typeof this.album[this.currentImageIndex].title !== 'undefined' && this.album[this.currentImageIndex].title !== "") {
326 this.$lightbox.find('.lb-caption')
327 .html(this.album[this.currentImageIndex].title)
329 .find('a').on('click', function(event){
330 location.href = $(this).attr('href');
334 if (this.album.length > 1 && this.options.showImageNumberLabel) {
335 this.$lightbox.find('.lb-number').text(this.options.albumLabel(this.currentImageIndex + 1, this.album.length)).fadeIn('fast');
337 this.$lightbox.find('.lb-number').hide();
340 this.$outerContainer.removeClass('animating');
342 this.$lightbox.find('.lb-dataContainer').fadeIn(this.options.resizeDuration, function() {
343 return self.sizeOverlay();
347 // Preload previous and next images in set.
348 Lightbox.prototype.preloadNeighboringImages = function() {
349 if (this.album.length > this.currentImageIndex + 1) {
350 var preloadNext = new Image();
351 preloadNext.src = this.album[this.currentImageIndex + 1].link;
353 if (this.currentImageIndex > 0) {
354 var preloadPrev = new Image();
355 preloadPrev.src = this.album[this.currentImageIndex - 1].link;
359 Lightbox.prototype.enableKeyboardNav = function() {
360 $(document).on('keyup.keyboard', $.proxy(this.keyboardAction, this));
363 Lightbox.prototype.disableKeyboardNav = function() {
364 $(document).off('.keyboard');
367 Lightbox.prototype.keyboardAction = function(event) {
368 var KEYCODE_ESC = 27;
369 var KEYCODE_LEFTARROW = 37;
370 var KEYCODE_RIGHTARROW = 39;
372 var keycode = event.keyCode;
373 var key = String.fromCharCode(keycode).toLowerCase();
374 if (keycode === KEYCODE_ESC || key.match(/x|o|c/)) {
376 } else if (key === 'p' || keycode === KEYCODE_LEFTARROW) {
377 if (this.currentImageIndex !== 0) {
378 this.changeImage(this.currentImageIndex - 1);
379 } else if (this.options.wrapAround && this.album.length > 1) {
380 this.changeImage(this.album.length - 1);
382 } else if (key === 'n' || keycode === KEYCODE_RIGHTARROW) {
383 if (this.currentImageIndex !== this.album.length - 1) {
384 this.changeImage(this.currentImageIndex + 1);
385 } else if (this.options.wrapAround && this.album.length > 1) {
392 Lightbox.prototype.end = function() {
393 this.disableKeyboardNav();
394 $(window).off("resize", this.sizeOverlay);
395 this.$lightbox.fadeOut(this.options.fadeDuration);
396 this.$overlay.fadeOut(this.options.fadeDuration);
397 $('select, object, embed').css({
398 visibility: "visible"
407 var options = new LightboxOptions();
408 var lightbox = new Lightbox(options);