lightgallery.js 43 KB

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