lightgallery.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*! lightgallery - v1.2.21 - 2016-06-28
  2. * http://sachinchoolur.github.io/lightGallery/
  3. * Copyright (c) 2016 Sachin N; Licensed Apache 2.0 */
  4. (function($, window, document, undefined) {
  5. 'use strict';
  6. var defaults = {
  7. mode: 'lg-slide',
  8. // Ex : 'ease'
  9. cssEasing: 'ease',
  10. //'for jquery animation'
  11. easing: 'linear',
  12. speed: 600,
  13. height: '100%',
  14. width: '100%',
  15. addClass: '',
  16. startClass: 'lg-start-zoom',
  17. backdropDuration: 150,
  18. hideBarsDelay: 6000,
  19. useLeft: false,
  20. closable: true,
  21. loop: true,
  22. escKey: true,
  23. keyPress: true,
  24. controls: true,
  25. slideEndAnimatoin: true,
  26. hideControlOnEnd: false,
  27. mousewheel: true,
  28. getCaptionFromTitleOrAlt: true,
  29. // .lg-item || '.lg-sub-html'
  30. appendSubHtmlTo: '.lg-sub-html',
  31. subHtmlSelectorRelative: false,
  32. /**
  33. * @desc number of preload slides
  34. * will exicute only after the current slide is fully loaded.
  35. *
  36. * @ex you clicked on 4th image and if preload = 1 then 3rd slide and 5th
  37. * slide will be loaded in the background after the 4th slide is fully loaded..
  38. * if preload is 2 then 2nd 3rd 5th 6th slides will be preloaded.. ... ...
  39. *
  40. */
  41. preload: 1,
  42. showAfterLoad: true,
  43. selector: '',
  44. selectWithin: '',
  45. nextHtml: '',
  46. prevHtml: '',
  47. // 0, 1
  48. index: false,
  49. iframeMaxWidth: '100%',
  50. download: true,
  51. counter: true,
  52. appendCounterTo: '.lg-toolbar',
  53. swipeThreshold: 50,
  54. enableSwipe: true,
  55. enableDrag: true,
  56. dynamic: false,
  57. dynamicEl: [],
  58. galleryId: 1
  59. };
  60. function Plugin(element, options) {
  61. // Current lightGallery element
  62. this.el = element;
  63. // Current jquery element
  64. this.$el = $(element);
  65. // lightGallery settings
  66. this.s = $.extend({}, defaults, options);
  67. // When using dynamic mode, ensure dynamicEl is an array
  68. if (this.s.dynamic && this.s.dynamicEl !== 'undefined' && this.s.dynamicEl.constructor === Array && !this.s.dynamicEl.length) {
  69. throw ('When using dynamic mode, you must also define dynamicEl as an Array.');
  70. }
  71. // lightGallery modules
  72. this.modules = {};
  73. // false when lightgallery complete first slide;
  74. this.lGalleryOn = false;
  75. this.lgBusy = false;
  76. // Timeout function for hiding controls;
  77. this.hideBartimeout = false;
  78. // To determine browser supports for touch events;
  79. this.isTouch = ('ontouchstart' in document.documentElement);
  80. // Disable hideControlOnEnd if sildeEndAnimation is true
  81. if (this.s.slideEndAnimatoin) {
  82. this.s.hideControlOnEnd = false;
  83. }
  84. // Gallery items
  85. if (this.s.dynamic) {
  86. this.$items = this.s.dynamicEl;
  87. } else {
  88. if (this.s.selector === 'this') {
  89. this.$items = this.$el;
  90. } else if (this.s.selector !== '') {
  91. if (this.s.selectWithin) {
  92. this.$items = $(this.s.selectWithin).find(this.s.selector);
  93. } else {
  94. this.$items = this.$el.find($(this.s.selector));
  95. }
  96. } else {
  97. this.$items = this.$el.children();
  98. }
  99. }
  100. // .lg-item
  101. this.$slide = '';
  102. // .lg-outer
  103. this.$outer = '';
  104. this.init();
  105. return this;
  106. }
  107. Plugin.prototype.init = function() {
  108. var _this = this;
  109. // s.preload should not be more than $item.length
  110. if (_this.s.preload > _this.$items.length) {
  111. _this.s.preload = _this.$items.length;
  112. }
  113. // if dynamic option is enabled execute immediately
  114. var _hash = window.location.hash;
  115. if (_hash.indexOf('lg=' + this.s.galleryId) > 0) {
  116. _this.index = parseInt(_hash.split('&slide=')[1], 10);
  117. $('body').addClass('lg-from-hash');
  118. if (!$('body').hasClass('lg-on')) {
  119. setTimeout(function() {
  120. _this.build(_this.index);
  121. $('body').addClass('lg-on');
  122. });
  123. }
  124. }
  125. if (_this.s.dynamic) {
  126. _this.$el.trigger('onBeforeOpen.lg');
  127. _this.index = _this.s.index || 0;
  128. // prevent accidental double execution
  129. if (!$('body').hasClass('lg-on')) {
  130. setTimeout(function() {
  131. _this.build(_this.index);
  132. $('body').addClass('lg-on');
  133. });
  134. }
  135. } else {
  136. // Using different namespace for click because click event should not unbind if selector is same object('this')
  137. _this.$items.on('click.lgcustom', function(event) {
  138. // For IE8
  139. try {
  140. event.preventDefault();
  141. event.preventDefault();
  142. } catch (er) {
  143. event.returnValue = false;
  144. }
  145. _this.$el.trigger('onBeforeOpen.lg');
  146. _this.index = _this.s.index || _this.$items.index(this);
  147. // prevent accidental double execution
  148. if (!$('body').hasClass('lg-on')) {
  149. _this.build(_this.index);
  150. $('body').addClass('lg-on');
  151. }
  152. });
  153. }
  154. };
  155. Plugin.prototype.build = function(index) {
  156. var _this = this;
  157. _this.structure();
  158. // module constructor
  159. $.each($.fn.lightGallery.modules, function(key) {
  160. _this.modules[key] = new $.fn.lightGallery.modules[key](_this.el);
  161. });
  162. // initiate slide function
  163. _this.slide(index, false, false);
  164. if (_this.s.keyPress) {
  165. _this.keyPress();
  166. }
  167. if (_this.$items.length > 1) {
  168. _this.arrow();
  169. setTimeout(function() {
  170. _this.enableDrag();
  171. _this.enableSwipe();
  172. }, 50);
  173. if (_this.s.mousewheel) {
  174. _this.mousewheel();
  175. }
  176. }
  177. _this.counter();
  178. _this.closeGallery();
  179. _this.$el.trigger('onAfterOpen.lg');
  180. // Hide controllers if mouse doesn't move for some period
  181. _this.$outer.on('mousemove.lg click.lg touchstart.lg', function() {
  182. _this.$outer.removeClass('lg-hide-items');
  183. clearTimeout(_this.hideBartimeout);
  184. // Timeout will be cleared on each slide movement also
  185. _this.hideBartimeout = setTimeout(function() {
  186. _this.$outer.addClass('lg-hide-items');
  187. }, _this.s.hideBarsDelay);
  188. });
  189. };
  190. Plugin.prototype.structure = function() {
  191. var list = '';
  192. var controls = '';
  193. var i = 0;
  194. var subHtmlCont = '';
  195. var template;
  196. var _this = this;
  197. $('body').append('<div class="lg-backdrop"></div>');
  198. $('.lg-backdrop').css('transition-duration', this.s.backdropDuration + 'ms');
  199. // Create gallery items
  200. for (i = 0; i < this.$items.length; i++) {
  201. list += '<div class="lg-item"></div>';
  202. }
  203. // Create controlls
  204. if (this.s.controls && this.$items.length > 1) {
  205. controls = '<div class="lg-actions">' +
  206. '<div class="lg-prev lg-icon">' + this.s.prevHtml + '</div>' +
  207. '<div class="lg-next lg-icon">' + this.s.nextHtml + '</div>' +
  208. '</div>';
  209. }
  210. if (this.s.appendSubHtmlTo === '.lg-sub-html') {
  211. subHtmlCont = '<div class="lg-sub-html"></div>';
  212. }
  213. template = '<div class="lg-outer ' + this.s.addClass + ' ' + this.s.startClass + '">' +
  214. '<div class="lg" style="width:' + this.s.width + '; height:' + this.s.height + '">' +
  215. '<div class="lg-inner">' + list + '</div>' +
  216. '<div class="lg-toolbar group">' +
  217. '<span class="lg-close lg-icon"></span>' +
  218. '</div>' +
  219. controls +
  220. subHtmlCont +
  221. '</div>' +
  222. '</div>';
  223. $('body').append(template);
  224. this.$outer = $('.lg-outer');
  225. this.$slide = this.$outer.find('.lg-item');
  226. if (this.s.useLeft) {
  227. this.$outer.addClass('lg-use-left');
  228. // Set mode lg-slide if use left is true;
  229. this.s.mode = 'lg-slide';
  230. } else {
  231. this.$outer.addClass('lg-use-css3');
  232. }
  233. // For fixed height gallery
  234. _this.setTop();
  235. $(window).on('resize.lg orientationchange.lg', function() {
  236. setTimeout(function() {
  237. _this.setTop();
  238. }, 100);
  239. });
  240. // add class lg-current to remove initial transition
  241. this.$slide.eq(this.index).addClass('lg-current');
  242. // add Class for css support and transition mode
  243. if (this.doCss()) {
  244. this.$outer.addClass('lg-css3');
  245. } else {
  246. this.$outer.addClass('lg-css');
  247. // Set speed 0 because no animation will happen if browser doesn't support css3
  248. this.s.speed = 0;
  249. }
  250. this.$outer.addClass(this.s.mode);
  251. if (this.s.enableDrag && this.$items.length > 1) {
  252. this.$outer.addClass('lg-grab');
  253. }
  254. if (this.s.showAfterLoad) {
  255. this.$outer.addClass('lg-show-after-load');
  256. }
  257. if (this.doCss()) {
  258. var $inner = this.$outer.find('.lg-inner');
  259. $inner.css('transition-timing-function', this.s.cssEasing);
  260. $inner.css('transition-duration', this.s.speed + 'ms');
  261. }
  262. $('.lg-backdrop').addClass('in');
  263. setTimeout(function() {
  264. _this.$outer.addClass('lg-visible');
  265. }, this.s.backdropDuration);
  266. if (this.s.download) {
  267. this.$outer.find('.lg-toolbar').append('<a id="lg-download" target="_blank" download class="lg-download lg-icon"></a>');
  268. }
  269. // Store the current scroll top value to scroll back after closing the gallery..
  270. this.prevScrollTop = $(window).scrollTop();
  271. };
  272. // For fixed height gallery
  273. Plugin.prototype.setTop = function() {
  274. if (this.s.height !== '100%') {
  275. var wH = $(window).height();
  276. var top = (wH - parseInt(this.s.height, 10)) / 2;
  277. var $lGallery = this.$outer.find('.lg');
  278. if (wH >= parseInt(this.s.height, 10)) {
  279. $lGallery.css('top', top + 'px');
  280. } else {
  281. $lGallery.css('top', '0px');
  282. }
  283. }
  284. };
  285. // Find css3 support
  286. Plugin.prototype.doCss = function() {
  287. // check for css animation support
  288. var support = function() {
  289. var transition = ['transition', 'MozTransition', 'WebkitTransition', 'OTransition', 'msTransition', 'KhtmlTransition'];
  290. var root = document.documentElement;
  291. var i = 0;
  292. for (i = 0; i < transition.length; i++) {
  293. if (transition[i] in root.style) {
  294. return true;
  295. }
  296. }
  297. };
  298. if (support()) {
  299. return true;
  300. }
  301. return false;
  302. };
  303. /**
  304. * @desc Check the given src is video
  305. * @param {String} src
  306. * @return {Object} video type
  307. * Ex:{ youtube : ["//www.youtube.com/watch?v=c0asJgSyxcY", "c0asJgSyxcY"] }
  308. */
  309. Plugin.prototype.isVideo = function(src, index) {
  310. var html;
  311. if (this.s.dynamic) {
  312. html = this.s.dynamicEl[index].html;
  313. } else {
  314. html = this.$items.eq(index).attr('data-html');
  315. }
  316. if (!src && html) {
  317. return {
  318. html5: true
  319. };
  320. }
  321. var youtube = src.match(/\/\/(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=|embed\/)?([a-z0-9\-\_\%]+)/i);
  322. var vimeo = src.match(/\/\/(?:www\.)?vimeo.com\/([0-9a-z\-_]+)/i);
  323. var dailymotion = src.match(/\/\/(?:www\.)?dai.ly\/([0-9a-z\-_]+)/i);
  324. var vk = src.match(/\/\/(?:www\.)?(?:vk\.com|vkontakte\.ru)\/(?:video_ext\.php\?)(.*)/i);
  325. if (youtube) {
  326. return {
  327. youtube: youtube
  328. };
  329. } else if (vimeo) {
  330. return {
  331. vimeo: vimeo
  332. };
  333. } else if (dailymotion) {
  334. return {
  335. dailymotion: dailymotion
  336. };
  337. } else if (vk) {
  338. return {
  339. vk: vk
  340. };
  341. }
  342. };
  343. /**
  344. * @desc Create image counter
  345. * Ex: 1/10
  346. */
  347. Plugin.prototype.counter = function() {
  348. if (this.s.counter) {
  349. $(this.s.appendCounterTo).append('<div id="lg-counter"><span id="lg-counter-current">' + (parseInt(this.index, 10) + 1) + '</span> / <span id="lg-counter-all">' + this.$items.length + '</span></div>');
  350. }
  351. };
  352. /**
  353. * @desc add sub-html into the slide
  354. * @param {Number} index - index of the slide
  355. */
  356. Plugin.prototype.addHtml = function(index) {
  357. var subHtml = null;
  358. var subHtmlUrl;
  359. var $currentEle;
  360. if (this.s.dynamic) {
  361. if (this.s.dynamicEl[index].subHtmlUrl) {
  362. subHtmlUrl = this.s.dynamicEl[index].subHtmlUrl;
  363. } else {
  364. subHtml = this.s.dynamicEl[index].subHtml;
  365. }
  366. } else {
  367. $currentEle = this.$items.eq(index);
  368. if ($currentEle.attr('data-sub-html-url')) {
  369. subHtmlUrl = $currentEle.attr('data-sub-html-url');
  370. } else {
  371. subHtml = $currentEle.attr('data-sub-html');
  372. if (this.s.getCaptionFromTitleOrAlt && !subHtml) {
  373. subHtml = $currentEle.attr('title') || $currentEle.find('img').first().attr('alt');
  374. }
  375. }
  376. }
  377. if (!subHtmlUrl) {
  378. if (typeof subHtml !== 'undefined' && subHtml !== null) {
  379. // get first letter of subhtml
  380. // if first letter starts with . or # get the html form the jQuery object
  381. var fL = subHtml.substring(0, 1);
  382. if (fL === '.' || fL === '#') {
  383. if (this.s.subHtmlSelectorRelative && !this.s.dynamic) {
  384. subHtml = $currentEle.find(subHtml).html();
  385. } else {
  386. subHtml = $(subHtml).html();
  387. }
  388. }
  389. } else {
  390. subHtml = '';
  391. }
  392. }
  393. if (this.s.appendSubHtmlTo === '.lg-sub-html') {
  394. if (subHtmlUrl) {
  395. this.$outer.find(this.s.appendSubHtmlTo).load(subHtmlUrl);
  396. } else {
  397. this.$outer.find(this.s.appendSubHtmlTo).html(subHtml);
  398. }
  399. } else {
  400. if (subHtmlUrl) {
  401. this.$slide.eq(index).load(subHtmlUrl);
  402. } else {
  403. this.$slide.eq(index).append(subHtml);
  404. }
  405. }
  406. // Add lg-empty-html class if title doesn't exist
  407. if (typeof subHtml !== 'undefined' && subHtml !== null) {
  408. if (subHtml === '') {
  409. this.$outer.find(this.s.appendSubHtmlTo).addClass('lg-empty-html');
  410. } else {
  411. this.$outer.find(this.s.appendSubHtmlTo).removeClass('lg-empty-html');
  412. }
  413. }
  414. this.$el.trigger('onAfterAppendSubHtml.lg', [index]);
  415. };
  416. /**
  417. * @desc Preload slides
  418. * @param {Number} index - index of the slide
  419. */
  420. Plugin.prototype.preload = function(index) {
  421. var i = 1;
  422. var j = 1;
  423. for (i = 1; i <= this.s.preload; i++) {
  424. if (i >= this.$items.length - index) {
  425. break;
  426. }
  427. this.loadContent(index + i, false, 0);
  428. }
  429. for (j = 1; j <= this.s.preload; j++) {
  430. if (index - j < 0) {
  431. break;
  432. }
  433. this.loadContent(index - j, false, 0);
  434. }
  435. };
  436. /**
  437. * @desc Load slide content into slide.
  438. * @param {Number} index - index of the slide.
  439. * @param {Boolean} rec - if true call loadcontent() function again.
  440. * @param {Boolean} delay - delay for adding complete class. it is 0 except first time.
  441. */
  442. Plugin.prototype.loadContent = function(index, rec, delay) {
  443. var _this = this;
  444. var _hasPoster = false;
  445. var _$img;
  446. var _src;
  447. var _poster;
  448. var _srcset;
  449. var _sizes;
  450. var _html;
  451. var getResponsiveSrc = function(srcItms) {
  452. var rsWidth = [];
  453. var rsSrc = [];
  454. for (var i = 0; i < srcItms.length; i++) {
  455. var __src = srcItms[i].split(' ');
  456. // Manage empty space
  457. if (__src[0] === '') {
  458. __src.splice(0, 1);
  459. }
  460. rsSrc.push(__src[0]);
  461. rsWidth.push(__src[1]);
  462. }
  463. var wWidth = $(window).width();
  464. for (var j = 0; j < rsWidth.length; j++) {
  465. if (parseInt(rsWidth[j], 10) > wWidth) {
  466. _src = rsSrc[j];
  467. break;
  468. }
  469. }
  470. };
  471. if (_this.s.dynamic) {
  472. if (_this.s.dynamicEl[index].poster) {
  473. _hasPoster = true;
  474. _poster = _this.s.dynamicEl[index].poster;
  475. }
  476. _html = _this.s.dynamicEl[index].html;
  477. _src = _this.s.dynamicEl[index].src;
  478. if (_this.s.dynamicEl[index].responsive) {
  479. var srcDyItms = _this.s.dynamicEl[index].responsive.split(',');
  480. getResponsiveSrc(srcDyItms);
  481. }
  482. _srcset = _this.s.dynamicEl[index].srcset;
  483. _sizes = _this.s.dynamicEl[index].sizes;
  484. } else {
  485. if (_this.$items.eq(index).attr('data-poster')) {
  486. _hasPoster = true;
  487. _poster = _this.$items.eq(index).attr('data-poster');
  488. }
  489. _html = _this.$items.eq(index).attr('data-html');
  490. _src = _this.$items.eq(index).attr('href') || _this.$items.eq(index).attr('data-src');
  491. if (_this.$items.eq(index).attr('data-responsive')) {
  492. var srcItms = _this.$items.eq(index).attr('data-responsive').split(',');
  493. getResponsiveSrc(srcItms);
  494. }
  495. _srcset = _this.$items.eq(index).attr('data-srcset');
  496. _sizes = _this.$items.eq(index).attr('data-sizes');
  497. }
  498. //if (_src || _srcset || _sizes || _poster) {
  499. var iframe = false;
  500. if (_this.s.dynamic) {
  501. if (_this.s.dynamicEl[index].iframe) {
  502. iframe = true;
  503. }
  504. } else {
  505. if (_this.$items.eq(index).attr('data-iframe') === 'true') {
  506. iframe = true;
  507. }
  508. }
  509. var _isVideo = _this.isVideo(_src, index);
  510. if (!_this.$slide.eq(index).hasClass('lg-loaded')) {
  511. if (iframe) {
  512. _this.$slide.eq(index).prepend('<div class="lg-video-cont" style="max-width:' + _this.s.iframeMaxWidth + '"><div class="lg-video"><iframe class="lg-object" frameborder="0" src="' + _src + '" allowfullscreen="true"></iframe></div></div>');
  513. } else if (_hasPoster) {
  514. var videoClass = '';
  515. if (_isVideo && _isVideo.youtube) {
  516. videoClass = 'lg-has-youtube';
  517. } else if (_isVideo && _isVideo.vimeo) {
  518. videoClass = 'lg-has-vimeo';
  519. } else {
  520. videoClass = 'lg-has-html5';
  521. }
  522. _this.$slide.eq(index).prepend('<div class="lg-video-cont ' + videoClass + ' "><div class="lg-video"><span class="lg-video-play"></span><img class="lg-object lg-has-poster" src="' + _poster + '" /></div></div>');
  523. } else if (_isVideo) {
  524. _this.$slide.eq(index).prepend('<div class="lg-video-cont "><div class="lg-video"></div></div>');
  525. _this.$el.trigger('hasVideo.lg', [index, _src, _html]);
  526. } else {
  527. _this.$slide.eq(index).prepend('<div class="lg-img-wrap"><img class="lg-object lg-image" src="' + _src + '" /></div>');
  528. }
  529. _this.$el.trigger('onAferAppendSlide.lg', [index]);
  530. _$img = _this.$slide.eq(index).find('.lg-object');
  531. if (_sizes) {
  532. _$img.attr('sizes', _sizes);
  533. }
  534. if (_srcset) {
  535. _$img.attr('srcset', _srcset);
  536. try {
  537. picturefill({
  538. elements: [_$img[0]]
  539. });
  540. } catch (e) {
  541. console.error('Make sure you have included Picturefill version 2');
  542. }
  543. }
  544. if (this.s.appendSubHtmlTo !== '.lg-sub-html') {
  545. _this.addHtml(index);
  546. }
  547. _this.$slide.eq(index).addClass('lg-loaded');
  548. }
  549. _this.$slide.eq(index).find('.lg-object').on('load.lg error.lg', function() {
  550. // For first time add some delay for displaying the start animation.
  551. var _speed = 0;
  552. // Do not change the delay value because it is required for zoom plugin.
  553. // If gallery opened from direct url (hash) speed value should be 0
  554. if (delay && !$('body').hasClass('lg-from-hash')) {
  555. _speed = delay;
  556. }
  557. setTimeout(function() {
  558. _this.$slide.eq(index).addClass('lg-complete');
  559. _this.$el.trigger('onSlideItemLoad.lg', [index, delay || 0]);
  560. }, _speed);
  561. });
  562. // @todo check load state for html5 videos
  563. if (_isVideo && _isVideo.html5 && !_hasPoster) {
  564. _this.$slide.eq(index).addClass('lg-complete');
  565. }
  566. if (rec === true) {
  567. if (!_this.$slide.eq(index).hasClass('lg-complete')) {
  568. _this.$slide.eq(index).find('.lg-object').on('load.lg error.lg', function() {
  569. _this.preload(index);
  570. });
  571. } else {
  572. _this.preload(index);
  573. }
  574. }
  575. //}
  576. };
  577. /**
  578. * @desc slide function for lightgallery
  579. ** Slide() gets call on start
  580. ** ** Set lg.on true once slide() function gets called.
  581. ** Call loadContent() on slide() function inside setTimeout
  582. ** ** On first slide we do not want any animation like slide of fade
  583. ** ** So on first slide( if lg.on if false that is first slide) loadContent() should start loading immediately
  584. ** ** Else loadContent() should wait for the transition to complete.
  585. ** ** So set timeout s.speed + 50
  586. <=> ** loadContent() will load slide content in to the particular slide
  587. ** ** It has recursion (rec) parameter. if rec === true loadContent() will call preload() function.
  588. ** ** preload will execute only when the previous slide is fully loaded (images iframe)
  589. ** ** avoid simultaneous image load
  590. <=> ** Preload() will check for s.preload value and call loadContent() again accoring to preload value
  591. ** loadContent() <====> Preload();
  592. * @param {Number} index - index of the slide
  593. * @param {Boolean} fromTouch - true if slide function called via touch event or mouse drag
  594. * @param {Boolean} fromThumb - true if slide function called via thumbnail click
  595. */
  596. Plugin.prototype.slide = function(index, fromTouch, fromThumb) {
  597. var _prevIndex = this.$outer.find('.lg-current').index();
  598. var _this = this;
  599. // Prevent if multiple call
  600. // Required for hsh plugin
  601. if (_this.lGalleryOn && (_prevIndex === index)) {
  602. return;
  603. }
  604. var _length = this.$slide.length;
  605. var _time = _this.lGalleryOn ? this.s.speed : 0;
  606. var _next = false;
  607. var _prev = false;
  608. if (!_this.lgBusy) {
  609. if (this.s.download) {
  610. var _src;
  611. if (_this.s.dynamic) {
  612. _src = _this.s.dynamicEl[index].downloadUrl !== false && (_this.s.dynamicEl[index].downloadUrl || _this.s.dynamicEl[index].src);
  613. } else {
  614. _src = _this.$items.eq(index).attr('data-download-url') !== 'false' && (_this.$items.eq(index).attr('data-download-url') || _this.$items.eq(index).attr('href') || _this.$items.eq(index).attr('data-src'));
  615. }
  616. if (_src) {
  617. $('#lg-download').attr('href', _src);
  618. _this.$outer.removeClass('lg-hide-download');
  619. } else {
  620. _this.$outer.addClass('lg-hide-download');
  621. }
  622. }
  623. this.$el.trigger('onBeforeSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
  624. _this.lgBusy = true;
  625. clearTimeout(_this.hideBartimeout);
  626. // Add title if this.s.appendSubHtmlTo === lg-sub-html
  627. if (this.s.appendSubHtmlTo === '.lg-sub-html') {
  628. // wait for slide animation to complete
  629. setTimeout(function() {
  630. _this.addHtml(index);
  631. }, _time);
  632. }
  633. this.arrowDisable(index);
  634. if (!fromTouch) {
  635. // remove all transitions
  636. _this.$outer.addClass('lg-no-trans');
  637. this.$slide.removeClass('lg-prev-slide lg-next-slide');
  638. if (index < _prevIndex) {
  639. _prev = true;
  640. if ((index === 0) && (_prevIndex === _length - 1) && !fromThumb) {
  641. _prev = false;
  642. _next = true;
  643. }
  644. } else if (index > _prevIndex) {
  645. _next = true;
  646. if ((index === _length - 1) && (_prevIndex === 0) && !fromThumb) {
  647. _prev = true;
  648. _next = false;
  649. }
  650. }
  651. if (_prev) {
  652. //prevslide
  653. this.$slide.eq(index).addClass('lg-prev-slide');
  654. this.$slide.eq(_prevIndex).addClass('lg-next-slide');
  655. } else if (_next) {
  656. // next slide
  657. this.$slide.eq(index).addClass('lg-next-slide');
  658. this.$slide.eq(_prevIndex).addClass('lg-prev-slide');
  659. }
  660. // give 50 ms for browser to add/remove class
  661. setTimeout(function() {
  662. _this.$slide.removeClass('lg-current');
  663. //_this.$slide.eq(_prevIndex).removeClass('lg-current');
  664. _this.$slide.eq(index).addClass('lg-current');
  665. // reset all transitions
  666. _this.$outer.removeClass('lg-no-trans');
  667. }, 50);
  668. } else {
  669. var touchPrev = index - 1;
  670. var touchNext = index + 1;
  671. if ((index === 0) && (_prevIndex === _length - 1)) {
  672. // next slide
  673. touchNext = 0;
  674. touchPrev = _length - 1;
  675. } else if ((index === _length - 1) && (_prevIndex === 0)) {
  676. // prev slide
  677. touchNext = 0;
  678. touchPrev = _length - 1;
  679. }
  680. this.$slide.removeClass('lg-prev-slide lg-current lg-next-slide');
  681. _this.$slide.eq(touchPrev).addClass('lg-prev-slide');
  682. _this.$slide.eq(touchNext).addClass('lg-next-slide');
  683. _this.$slide.eq(index).addClass('lg-current');
  684. }
  685. if (_this.lGalleryOn) {
  686. setTimeout(function() {
  687. _this.loadContent(index, true, 0);
  688. }, this.s.speed + 50);
  689. setTimeout(function() {
  690. _this.lgBusy = false;
  691. _this.$el.trigger('onAfterSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
  692. }, this.s.speed);
  693. } else {
  694. _this.loadContent(index, true, _this.s.backdropDuration);
  695. _this.lgBusy = false;
  696. _this.$el.trigger('onAfterSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
  697. }
  698. _this.lGalleryOn = true;
  699. if (this.s.counter) {
  700. $('#lg-counter-current').text(index + 1);
  701. }
  702. }
  703. };
  704. /**
  705. * @desc Go to next slide
  706. * @param {Boolean} fromTouch - true if slide function called via touch event
  707. */
  708. Plugin.prototype.goToNextSlide = function(fromTouch) {
  709. var _this = this;
  710. if (!_this.lgBusy) {
  711. if ((_this.index + 1) < _this.$slide.length) {
  712. _this.index++;
  713. _this.$el.trigger('onBeforeNextSlide.lg', [_this.index]);
  714. _this.slide(_this.index, fromTouch, false);
  715. } else {
  716. if (_this.s.loop) {
  717. _this.index = 0;
  718. _this.$el.trigger('onBeforeNextSlide.lg', [_this.index]);
  719. _this.slide(_this.index, fromTouch, false);
  720. } else if (_this.s.slideEndAnimatoin) {
  721. _this.$outer.addClass('lg-right-end');
  722. setTimeout(function() {
  723. _this.$outer.removeClass('lg-right-end');
  724. }, 400);
  725. }
  726. }
  727. }
  728. };
  729. /**
  730. * @desc Go to previous slide
  731. * @param {Boolean} fromTouch - true if slide function called via touch event
  732. */
  733. Plugin.prototype.goToPrevSlide = function(fromTouch) {
  734. var _this = this;
  735. if (!_this.lgBusy) {
  736. if (_this.index > 0) {
  737. _this.index--;
  738. _this.$el.trigger('onBeforePrevSlide.lg', [_this.index, fromTouch]);
  739. _this.slide(_this.index, fromTouch, false);
  740. } else {
  741. if (_this.s.loop) {
  742. _this.index = _this.$items.length - 1;
  743. _this.$el.trigger('onBeforePrevSlide.lg', [_this.index, fromTouch]);
  744. _this.slide(_this.index, fromTouch, false);
  745. } else if (_this.s.slideEndAnimatoin) {
  746. _this.$outer.addClass('lg-left-end');
  747. setTimeout(function() {
  748. _this.$outer.removeClass('lg-left-end');
  749. }, 400);
  750. }
  751. }
  752. }
  753. };
  754. Plugin.prototype.keyPress = function() {
  755. var _this = this;
  756. if (this.$items.length > 1) {
  757. $(window).on('keyup.lg', function(e) {
  758. if (_this.$items.length > 1) {
  759. if (e.keyCode === 37) {
  760. e.preventDefault();
  761. _this.goToPrevSlide();
  762. }
  763. if (e.keyCode === 39) {
  764. e.preventDefault();
  765. _this.goToNextSlide();
  766. }
  767. }
  768. });
  769. }
  770. $(window).on('keydown.lg', function(e) {
  771. if (_this.s.escKey === true && e.keyCode === 27) {
  772. e.preventDefault();
  773. if (!_this.$outer.hasClass('lg-thumb-open')) {
  774. _this.destroy();
  775. } else {
  776. _this.$outer.removeClass('lg-thumb-open');
  777. }
  778. }
  779. });
  780. };
  781. Plugin.prototype.arrow = function() {
  782. var _this = this;
  783. this.$outer.find('.lg-prev').on('click.lg', function() {
  784. _this.goToPrevSlide();
  785. });
  786. this.$outer.find('.lg-next').on('click.lg', function() {
  787. _this.goToNextSlide();
  788. });
  789. };
  790. Plugin.prototype.arrowDisable = function(index) {
  791. // Disable arrows if s.hideControlOnEnd is true
  792. if (!this.s.loop && this.s.hideControlOnEnd) {
  793. if ((index + 1) < this.$slide.length) {
  794. this.$outer.find('.lg-next').removeAttr('disabled').removeClass('disabled');
  795. } else {
  796. this.$outer.find('.lg-next').attr('disabled', 'disabled').addClass('disabled');
  797. }
  798. if (index > 0) {
  799. this.$outer.find('.lg-prev').removeAttr('disabled').removeClass('disabled');
  800. } else {
  801. this.$outer.find('.lg-prev').attr('disabled', 'disabled').addClass('disabled');
  802. }
  803. }
  804. };
  805. Plugin.prototype.setTranslate = function($el, xValue, yValue) {
  806. // jQuery supports Automatic CSS prefixing since jQuery 1.8.0
  807. if (this.s.useLeft) {
  808. $el.css('left', xValue);
  809. } else {
  810. $el.css({
  811. transform: 'translate3d(' + (xValue) + 'px, ' + yValue + 'px, 0px)'
  812. });
  813. }
  814. };
  815. Plugin.prototype.touchMove = function(startCoords, endCoords) {
  816. var distance = endCoords - startCoords;
  817. if (Math.abs(distance) > 15) {
  818. // reset opacity and transition duration
  819. this.$outer.addClass('lg-dragging');
  820. // move current slide
  821. this.setTranslate(this.$slide.eq(this.index), distance, 0);
  822. // move next and prev slide with current slide
  823. this.setTranslate($('.lg-prev-slide'), -this.$slide.eq(this.index).width() + distance, 0);
  824. this.setTranslate($('.lg-next-slide'), this.$slide.eq(this.index).width() + distance, 0);
  825. }
  826. };
  827. Plugin.prototype.touchEnd = function(distance) {
  828. var _this = this;
  829. // keep slide animation for any mode while dragg/swipe
  830. if (_this.s.mode !== 'lg-slide') {
  831. _this.$outer.addClass('lg-slide');
  832. }
  833. this.$slide.not('.lg-current, .lg-prev-slide, .lg-next-slide').css('opacity', '0');
  834. // set transition duration
  835. setTimeout(function() {
  836. _this.$outer.removeClass('lg-dragging');
  837. if ((distance < 0) && (Math.abs(distance) > _this.s.swipeThreshold)) {
  838. _this.goToNextSlide(true);
  839. } else if ((distance > 0) && (Math.abs(distance) > _this.s.swipeThreshold)) {
  840. _this.goToPrevSlide(true);
  841. } else if (Math.abs(distance) < 5) {
  842. // Trigger click if distance is less than 5 pix
  843. _this.$el.trigger('onSlideClick.lg');
  844. }
  845. _this.$slide.removeAttr('style');
  846. });
  847. // remove slide class once drag/swipe is completed if mode is not slide
  848. setTimeout(function() {
  849. if (!_this.$outer.hasClass('lg-dragging') && _this.s.mode !== 'lg-slide') {
  850. _this.$outer.removeClass('lg-slide');
  851. }
  852. }, _this.s.speed + 100);
  853. };
  854. Plugin.prototype.enableSwipe = function() {
  855. var _this = this;
  856. var startCoords = 0;
  857. var endCoords = 0;
  858. var isMoved = false;
  859. if (_this.s.enableSwipe && _this.isTouch && _this.doCss()) {
  860. _this.$slide.on('touchstart.lg', function(e) {
  861. if (!_this.$outer.hasClass('lg-zoomed') && !_this.lgBusy) {
  862. e.preventDefault();
  863. _this.manageSwipeClass();
  864. startCoords = e.originalEvent.targetTouches[0].pageX;
  865. }
  866. });
  867. _this.$slide.on('touchmove.lg', function(e) {
  868. if (!_this.$outer.hasClass('lg-zoomed')) {
  869. e.preventDefault();
  870. endCoords = e.originalEvent.targetTouches[0].pageX;
  871. _this.touchMove(startCoords, endCoords);
  872. isMoved = true;
  873. }
  874. });
  875. _this.$slide.on('touchend.lg', function() {
  876. if (!_this.$outer.hasClass('lg-zoomed')) {
  877. if (isMoved) {
  878. isMoved = false;
  879. _this.touchEnd(endCoords - startCoords);
  880. } else {
  881. _this.$el.trigger('onSlideClick.lg');
  882. }
  883. }
  884. });
  885. }
  886. };
  887. Plugin.prototype.enableDrag = function() {
  888. var _this = this;
  889. var startCoords = 0;
  890. var endCoords = 0;
  891. var isDraging = false;
  892. var isMoved = false;
  893. if (_this.s.enableDrag && !_this.isTouch && _this.doCss()) {
  894. _this.$slide.on('mousedown.lg', function(e) {
  895. // execute only on .lg-object
  896. if (!_this.$outer.hasClass('lg-zoomed')) {
  897. if ($(e.target).hasClass('lg-object') || $(e.target).hasClass('lg-video-play')) {
  898. e.preventDefault();
  899. if (!_this.lgBusy) {
  900. _this.manageSwipeClass();
  901. startCoords = e.pageX;
  902. isDraging = true;
  903. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  904. _this.$outer.scrollLeft += 1;
  905. _this.$outer.scrollLeft -= 1;
  906. // *
  907. _this.$outer.removeClass('lg-grab').addClass('lg-grabbing');
  908. _this.$el.trigger('onDragstart.lg');
  909. }
  910. }
  911. }
  912. });
  913. $(window).on('mousemove.lg', function(e) {
  914. if (isDraging) {
  915. isMoved = true;
  916. endCoords = e.pageX;
  917. _this.touchMove(startCoords, endCoords);
  918. _this.$el.trigger('onDragmove.lg');
  919. }
  920. });
  921. $(window).on('mouseup.lg', function(e) {
  922. if (isMoved) {
  923. isMoved = false;
  924. _this.touchEnd(endCoords - startCoords);
  925. _this.$el.trigger('onDragend.lg');
  926. } else if ($(e.target).hasClass('lg-object') || $(e.target).hasClass('lg-video-play')) {
  927. _this.$el.trigger('onSlideClick.lg');
  928. }
  929. // Prevent execution on click
  930. if (isDraging) {
  931. isDraging = false;
  932. _this.$outer.removeClass('lg-grabbing').addClass('lg-grab');
  933. }
  934. });
  935. }
  936. };
  937. Plugin.prototype.manageSwipeClass = function() {
  938. var touchNext = this.index + 1;
  939. var touchPrev = this.index - 1;
  940. var length = this.$slide.length;
  941. if (this.s.loop) {
  942. if (this.index === 0) {
  943. touchPrev = length - 1;
  944. } else if (this.index === length - 1) {
  945. touchNext = 0;
  946. }
  947. }
  948. this.$slide.removeClass('lg-next-slide lg-prev-slide');
  949. if (touchPrev > -1) {
  950. this.$slide.eq(touchPrev).addClass('lg-prev-slide');
  951. }
  952. this.$slide.eq(touchNext).addClass('lg-next-slide');
  953. };
  954. Plugin.prototype.mousewheel = function() {
  955. var _this = this;
  956. _this.$outer.on('mousewheel.lg', function(e) {
  957. if (!e.deltaY) {
  958. return;
  959. }
  960. if (e.deltaY > 0) {
  961. _this.goToPrevSlide();
  962. } else {
  963. _this.goToNextSlide();
  964. }
  965. e.preventDefault();
  966. });
  967. };
  968. Plugin.prototype.closeGallery = function() {
  969. var _this = this;
  970. var mousedown = false;
  971. this.$outer.find('.lg-close').on('click.lg', function() {
  972. _this.destroy();
  973. });
  974. if (_this.s.closable) {
  975. // If you drag the slide and release outside gallery gets close on chrome
  976. // for preventing this check mousedown and mouseup happened on .lg-item or lg-outer
  977. _this.$outer.on('mousedown.lg', function(e) {
  978. if ($(e.target).is('.lg-outer') || $(e.target).is('.lg-item ') || $(e.target).is('.lg-img-wrap')) {
  979. mousedown = true;
  980. } else {
  981. mousedown = false;
  982. }
  983. });
  984. _this.$outer.on('mouseup.lg', function(e) {
  985. if ($(e.target).is('.lg-outer') || $(e.target).is('.lg-item ') || $(e.target).is('.lg-img-wrap') && mousedown) {
  986. if (!_this.$outer.hasClass('lg-dragging')) {
  987. _this.destroy();
  988. }
  989. }
  990. });
  991. }
  992. };
  993. Plugin.prototype.destroy = function(d) {
  994. var _this = this;
  995. if (!d) {
  996. _this.$el.trigger('onBeforeClose.lg');
  997. }
  998. $(window).scrollTop(_this.prevScrollTop);
  999. /**
  1000. * if d is false or undefined destroy will only close the gallery
  1001. * plugins instance remains with the element
  1002. *
  1003. * if d is true destroy will completely remove the plugin
  1004. */
  1005. if (d) {
  1006. if (!_this.s.dynamic) {
  1007. // only when not using dynamic mode is $items a jquery collection
  1008. this.$items.off('click.lg click.lgcustom');
  1009. }
  1010. $.removeData(_this.el, 'lightGallery');
  1011. }
  1012. // Unbind all events added by lightGallery
  1013. this.$el.off('.lg.tm');
  1014. // Distroy all lightGallery modules
  1015. $.each($.fn.lightGallery.modules, function(key) {
  1016. if (_this.modules[key]) {
  1017. _this.modules[key].destroy();
  1018. }
  1019. });
  1020. this.lGalleryOn = false;
  1021. clearTimeout(_this.hideBartimeout);
  1022. this.hideBartimeout = false;
  1023. $(window).off('.lg');
  1024. $('body').removeClass('lg-on lg-from-hash');
  1025. if (_this.$outer) {
  1026. _this.$outer.removeClass('lg-visible');
  1027. }
  1028. $('.lg-backdrop').removeClass('in');
  1029. setTimeout(function() {
  1030. if (_this.$outer) {
  1031. _this.$outer.remove();
  1032. }
  1033. $('.lg-backdrop').remove();
  1034. if (!d) {
  1035. _this.$el.trigger('onCloseAfter.lg');
  1036. }
  1037. }, _this.s.backdropDuration + 50);
  1038. };
  1039. $.fn.lightGallery = function(options) {
  1040. return this.each(function() {
  1041. if (!$.data(this, 'lightGallery')) {
  1042. $.data(this, 'lightGallery', new Plugin(this, options));
  1043. } else {
  1044. try {
  1045. $(this).data('lightGallery').init();
  1046. } catch (err) {
  1047. console.error('lightGallery has not initiated properly');
  1048. }
  1049. }
  1050. });
  1051. };
  1052. $.fn.lightGallery.modules = {};
  1053. })(jQuery, window, document);