Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

lightbox.js 15KB

8 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * Lightbox v2.7.1
  3. * by Lokesh Dhakar - http://lokeshdhakar.com/projects/lightbox2/
  4. *
  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
  8. */
  9. (function() {
  10. // Use local alias
  11. var $ = jQuery;
  12. var LightboxOptions = (function() {
  13. function LightboxOptions() {
  14. this.fadeDuration = 500;
  15. this.fitImagesInViewport = true;
  16. this.resizeDuration = 700;
  17. this.positionFromTop = 50;
  18. this.showImageNumberLabel = true;
  19. this.alwaysShowNavOnTouchDevices = false;
  20. this.wrapAround = false;
  21. }
  22. // Change to localize to non-english language
  23. LightboxOptions.prototype.albumLabel = function(curImageNum, albumSize) {
  24. return "Image " + curImageNum + " of " + albumSize;
  25. };
  26. return LightboxOptions;
  27. })();
  28. var Lightbox = (function() {
  29. function Lightbox(options) {
  30. this.options = options;
  31. this.album = [];
  32. this.currentImageIndex = void 0;
  33. this.init();
  34. }
  35. Lightbox.prototype.init = function() {
  36. this.enable();
  37. this.build();
  38. };
  39. // Loop through anchors and areamaps looking for either data-lightbox attributes or rel attributes
  40. // that contain 'lightbox'. When these are clicked, start lightbox.
  41. Lightbox.prototype.enable = function() {
  42. var self = this;
  43. $('body').on('click', 'a[rel^=lightbox], area[rel^=lightbox], a[data-lightbox], area[data-lightbox]', function(event) {
  44. self.start($(event.currentTarget));
  45. return false;
  46. });
  47. };
  48. // Build html for the lightbox and the overlay.
  49. // Attach event handlers to the new DOM elements. click click click
  50. Lightbox.prototype.build = function() {
  51. var self = this;
  52. $("<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'));
  53. // Cache jQuery objects
  54. this.$lightbox = $('#lightbox');
  55. this.$overlay = $('#lightboxOverlay');
  56. this.$outerContainer = this.$lightbox.find('.lb-outerContainer');
  57. this.$container = this.$lightbox.find('.lb-container');
  58. // Store css values for future lookup
  59. this.containerTopPadding = parseInt(this.$container.css('padding-top'), 10);
  60. this.containerRightPadding = parseInt(this.$container.css('padding-right'), 10);
  61. this.containerBottomPadding = parseInt(this.$container.css('padding-bottom'), 10);
  62. this.containerLeftPadding = parseInt(this.$container.css('padding-left'), 10);
  63. // Attach event handlers to the newly minted DOM elements
  64. this.$overlay.hide().on('click', function() {
  65. self.end();
  66. return false;
  67. });
  68. this.$lightbox.hide().on('click', function(event) {
  69. if ($(event.target).attr('id') === 'lightbox') {
  70. self.end();
  71. }
  72. return false;
  73. });
  74. this.$outerContainer.on('click', function(event) {
  75. if ($(event.target).attr('id') === 'lightbox') {
  76. self.end();
  77. }
  78. return false;
  79. });
  80. this.$lightbox.find('.lb-prev').on('click', function() {
  81. if (self.currentImageIndex === 0) {
  82. self.changeImage(self.album.length - 1);
  83. } else {
  84. self.changeImage(self.currentImageIndex - 1);
  85. }
  86. return false;
  87. });
  88. this.$lightbox.find('.lb-next').on('click', function() {
  89. if (self.currentImageIndex === self.album.length - 1) {
  90. self.changeImage(0);
  91. } else {
  92. self.changeImage(self.currentImageIndex + 1);
  93. }
  94. return false;
  95. });
  96. this.$lightbox.find('.lb-loader, .lb-close').on('click', function() {
  97. self.end();
  98. return false;
  99. });
  100. };
  101. // Show overlay and lightbox. If the image is part of a set, add siblings to album array.
  102. Lightbox.prototype.start = function($link) {
  103. var self = this;
  104. var $window = $(window);
  105. $window.on('resize', $.proxy(this.sizeOverlay, this));
  106. $('select, object, embed').css({
  107. visibility: "hidden"
  108. });
  109. this.sizeOverlay();
  110. this.album = [];
  111. var imageNumber = 0;
  112. function addToAlbum($link) {
  113. self.album.push({
  114. link: $link.attr('href'),
  115. title: $link.attr('data-title') || $link.attr('title')
  116. });
  117. }
  118. // Support both data-lightbox attribute and rel attribute implementations
  119. var dataLightboxValue = $link.attr('data-lightbox');
  120. var $links;
  121. if (dataLightboxValue) {
  122. $links = $($link.prop("tagName") + '[data-lightbox="' + dataLightboxValue + '"]');
  123. for (var i = 0; i < $links.length; i = ++i) {
  124. addToAlbum($($links[i]));
  125. if ($links[i] === $link[0]) {
  126. imageNumber = i;
  127. }
  128. }
  129. } else {
  130. if ($link.attr('rel') === 'lightbox') {
  131. // If image is not part of a set
  132. addToAlbum($link);
  133. } else {
  134. // If image is part of a set
  135. $links = $($link.prop("tagName") + '[rel="' + $link.attr('rel') + '"]');
  136. for (var j = 0; j < $links.length; j = ++j) {
  137. addToAlbum($($links[j]));
  138. if ($links[j] === $link[0]) {
  139. imageNumber = j;
  140. }
  141. }
  142. }
  143. }
  144. // Position Lightbox
  145. var top = $window.scrollTop() + this.options.positionFromTop;
  146. var left = $window.scrollLeft();
  147. this.$lightbox.css({
  148. top: top + 'px',
  149. left: left + 'px'
  150. }).fadeIn(this.options.fadeDuration);
  151. this.changeImage(imageNumber);
  152. };
  153. // Hide most UI elements in preparation for the animated resizing of the lightbox.
  154. Lightbox.prototype.changeImage = function(imageNumber) {
  155. var self = this;
  156. this.disableKeyboardNav();
  157. var $image = this.$lightbox.find('.lb-image');
  158. this.$overlay.fadeIn(this.options.fadeDuration);
  159. $('.lb-loader').fadeIn('slow');
  160. this.$lightbox.find('.lb-image, .lb-nav, .lb-prev, .lb-next, .lb-dataContainer, .lb-numbers, .lb-caption').hide();
  161. this.$outerContainer.addClass('animating');
  162. // When image to show is preloaded, we send the width and height to sizeContainer()
  163. var preloader = new Image();
  164. preloader.onload = function() {
  165. var $preloader, imageHeight, imageWidth, maxImageHeight, maxImageWidth, windowHeight, windowWidth;
  166. $image.attr('src', self.album[imageNumber].link);
  167. $preloader = $(preloader);
  168. $image.width(preloader.width);
  169. $image.height(preloader.height);
  170. if (self.options.fitImagesInViewport) {
  171. // Fit image inside the viewport.
  172. // Take into account the border around the image and an additional 10px gutter on each side.
  173. windowWidth = $(window).width();
  174. windowHeight = $(window).height();
  175. maxImageWidth = windowWidth - self.containerLeftPadding - self.containerRightPadding - 20;
  176. maxImageHeight = windowHeight - self.containerTopPadding - self.containerBottomPadding - 120;
  177. // Is there a fitting issue?
  178. if ((preloader.width > maxImageWidth) || (preloader.height > maxImageHeight)) {
  179. if ((preloader.width / maxImageWidth) > (preloader.height / maxImageHeight)) {
  180. imageWidth = maxImageWidth;
  181. imageHeight = parseInt(preloader.height / (preloader.width / imageWidth), 10);
  182. $image.width(imageWidth);
  183. $image.height(imageHeight);
  184. } else {
  185. imageHeight = maxImageHeight;
  186. imageWidth = parseInt(preloader.width / (preloader.height / imageHeight), 10);
  187. $image.width(imageWidth);
  188. $image.height(imageHeight);
  189. }
  190. }
  191. }
  192. self.sizeContainer($image.width(), $image.height());
  193. };
  194. preloader.src = this.album[imageNumber].link;
  195. this.currentImageIndex = imageNumber;
  196. };
  197. // Stretch overlay to fit the viewport
  198. Lightbox.prototype.sizeOverlay = function() {
  199. this.$overlay
  200. .width($(window).width())
  201. .height($(document).height());
  202. };
  203. // Animate the size of the lightbox to fit the image we are showing
  204. Lightbox.prototype.sizeContainer = function(imageWidth, imageHeight) {
  205. var self = this;
  206. var oldWidth = this.$outerContainer.outerWidth();
  207. var oldHeight = this.$outerContainer.outerHeight();
  208. var newWidth = imageWidth + this.containerLeftPadding + this.containerRightPadding;
  209. var newHeight = imageHeight + this.containerTopPadding + this.containerBottomPadding;
  210. function postResize() {
  211. self.$lightbox.find('.lb-dataContainer').width(newWidth);
  212. self.$lightbox.find('.lb-prevLink').height(newHeight);
  213. self.$lightbox.find('.lb-nextLink').height(newHeight);
  214. self.showImage();
  215. }
  216. if (oldWidth !== newWidth || oldHeight !== newHeight) {
  217. this.$outerContainer.animate({
  218. width: newWidth,
  219. height: newHeight
  220. }, this.options.resizeDuration, 'swing', function() {
  221. postResize();
  222. });
  223. } else {
  224. postResize();
  225. }
  226. };
  227. // Display the image and it's details and begin preload neighboring images.
  228. Lightbox.prototype.showImage = function() {
  229. this.$lightbox.find('.lb-loader').hide();
  230. this.$lightbox.find('.lb-image').fadeIn('slow');
  231. this.updateNav();
  232. this.updateDetails();
  233. this.preloadNeighboringImages();
  234. this.enableKeyboardNav();
  235. };
  236. // Display previous and next navigation if appropriate.
  237. Lightbox.prototype.updateNav = function() {
  238. // Check to see if the browser supports touch events. If so, we take the conservative approach
  239. // and assume that mouse hover events are not supported and always show prev/next navigation
  240. // arrows in image sets.
  241. var alwaysShowNav = false;
  242. try {
  243. document.createEvent("TouchEvent");
  244. alwaysShowNav = (this.options.alwaysShowNavOnTouchDevices)? true: false;
  245. } catch (e) {}
  246. this.$lightbox.find('.lb-nav').show();
  247. if (this.album.length > 1) {
  248. if (this.options.wrapAround) {
  249. if (alwaysShowNav) {
  250. this.$lightbox.find('.lb-prev, .lb-next').css('opacity', '1');
  251. }
  252. this.$lightbox.find('.lb-prev, .lb-next').show();
  253. } else {
  254. if (this.currentImageIndex > 0) {
  255. this.$lightbox.find('.lb-prev').show();
  256. if (alwaysShowNav) {
  257. this.$lightbox.find('.lb-prev').css('opacity', '1');
  258. }
  259. }
  260. if (this.currentImageIndex < this.album.length - 1) {
  261. this.$lightbox.find('.lb-next').show();
  262. if (alwaysShowNav) {
  263. this.$lightbox.find('.lb-next').css('opacity', '1');
  264. }
  265. }
  266. }
  267. }
  268. };
  269. // Display caption, image number, and closing button.
  270. Lightbox.prototype.updateDetails = function() {
  271. var self = this;
  272. // Enable anchor clicks in the injected caption html.
  273. // Thanks Nate Wright for the fix. @https://github.com/NateWr
  274. if (typeof this.album[this.currentImageIndex].title !== 'undefined' && this.album[this.currentImageIndex].title !== "") {
  275. this.$lightbox.find('.lb-caption')
  276. .html(this.album[this.currentImageIndex].title)
  277. .fadeIn('fast')
  278. .find('a').on('click', function(event){
  279. location.href = $(this).attr('href');
  280. });
  281. }
  282. if (this.album.length > 1 && this.options.showImageNumberLabel) {
  283. this.$lightbox.find('.lb-number').text(this.options.albumLabel(this.currentImageIndex + 1, this.album.length)).fadeIn('fast');
  284. } else {
  285. this.$lightbox.find('.lb-number').hide();
  286. }
  287. this.$outerContainer.removeClass('animating');
  288. this.$lightbox.find('.lb-dataContainer').fadeIn(this.options.resizeDuration, function() {
  289. return self.sizeOverlay();
  290. });
  291. };
  292. // Preload previous and next images in set.
  293. Lightbox.prototype.preloadNeighboringImages = function() {
  294. if (this.album.length > this.currentImageIndex + 1) {
  295. var preloadNext = new Image();
  296. preloadNext.src = this.album[this.currentImageIndex + 1].link;
  297. }
  298. if (this.currentImageIndex > 0) {
  299. var preloadPrev = new Image();
  300. preloadPrev.src = this.album[this.currentImageIndex - 1].link;
  301. }
  302. };
  303. Lightbox.prototype.enableKeyboardNav = function() {
  304. $(document).on('keyup.keyboard', $.proxy(this.keyboardAction, this));
  305. };
  306. Lightbox.prototype.disableKeyboardNav = function() {
  307. $(document).off('.keyboard');
  308. };
  309. Lightbox.prototype.keyboardAction = function(event) {
  310. var KEYCODE_ESC = 27;
  311. var KEYCODE_LEFTARROW = 37;
  312. var KEYCODE_RIGHTARROW = 39;
  313. var keycode = event.keyCode;
  314. var key = String.fromCharCode(keycode).toLowerCase();
  315. if (keycode === KEYCODE_ESC || key.match(/x|o|c/)) {
  316. this.end();
  317. } else if (key === 'p' || keycode === KEYCODE_LEFTARROW) {
  318. if (this.currentImageIndex !== 0) {
  319. this.changeImage(this.currentImageIndex - 1);
  320. } else if (this.options.wrapAround && this.album.length > 1) {
  321. this.changeImage(this.album.length - 1);
  322. }
  323. } else if (key === 'n' || keycode === KEYCODE_RIGHTARROW) {
  324. if (this.currentImageIndex !== this.album.length - 1) {
  325. this.changeImage(this.currentImageIndex + 1);
  326. } else if (this.options.wrapAround && this.album.length > 1) {
  327. this.changeImage(0);
  328. }
  329. }
  330. };
  331. // Closing time. :-(
  332. Lightbox.prototype.end = function() {
  333. this.disableKeyboardNav();
  334. $(window).off("resize", this.sizeOverlay);
  335. this.$lightbox.fadeOut(this.options.fadeDuration);
  336. this.$overlay.fadeOut(this.options.fadeDuration);
  337. $('select, object, embed').css({
  338. visibility: "visible"
  339. });
  340. };
  341. return Lightbox;
  342. })();
  343. $(function() {
  344. var options = new LightboxOptions();
  345. var lightbox = new Lightbox(options);
  346. });
  347. }).call(this);