/** ========================================================== * jquery lightgallery.js v1.1.0 * http://sachinchoolur.github.io/lightgallery/ * released under the apache license - http://opensource.org/licenses/apache-2.0 =========================================================/**/ ; (function ($) { "use strict"; $.fn.lightgallery = function (options) { var defaults = { mode: 'slide', usecss: true, easing: 'linear', //'cubic-bezier(0.25, 0, 0.25, 1)',// speed: 1000, closable: true, loop: false, auto: false, pause: 4000, esckey: true, rel: false, lang: { allphotos: 'all photos' }, exthumbimage: false, index: false, thumbnail: true, caption: false, captionlink: false, desc: false, counter: false, controls: true, hidecontrolonend: false, mobilesrc: false, mobilesrcmaxwidth: 640, //touch swipethreshold: 50, vimeocolor: 'cccccc', videoautoplay: true, videomaxwidth: 855, dynamic: false, //callbacks dynamicel: [], onopen: function () {}, onslidebefore: function () {}, onslideafter: function () {}, onslidenext: function () {}, onslideprev: function () {}, onbeforeclose: function () {}, oncloseafter: function () {} }, el = $(this), $children, index, lightgalleryon = false, html = '
', istouch = document.createtouch !== undefined || ('ontouchstart' in window) || ('onmsgesturechange' in window) || navigator.msmaxtouchpoints, $gallery, $gallerycont, $slider, $slide, $prev, $next, previndex, $thumb_cont, $thumb, windowwidth, interval, usingthumb = false, atiming = false, aspeed = false; var settings = $.extend(true, {}, defaults, options); var lightgallery = { init: function () { el.each(function () { var $this = $(this); if (settings.dynamic == true) { $children = settings.dynamicel; index = 0; previndex = index; setup.init(index); } else { $children = $(this).children(); $children.click(function (e) { if (settings.rel == true && $this.data('rel')) { var rel = $this.data('rel'); $children = $('[data-rel="' + rel + '"]').children(); } else { $children = $this.children(); } e.preventdefault(); e.stoppropagation(); index = $children.index(this); previndex = index; setup.init(index); }); } }); }, }; var setup = { init: function () { this.start(); this.build(); }, start: function () { this.structure(); this.getwidth(); this.closeslide(); }, build: function () { this.loadcontent(index); this.addcaption(); this.adddesc(); //description this.counter(); this.slideto(); this.buildthumbnail(); this.keypress(); if(settings.index) { this.slide(settings.index); } else { this.slide(index); } this.touch(); this.enabletouch(); settimeout(function () { $gallery.addclass('opacity'); }, 50); }, structure: function () { $('body').append(html).addclass('lightgallery'); $gallerycont = $('#lightgallery-outer'); $gallery = $('#lightgallery-gallery'); $slider = $gallery.find('#lightgallery-slider'); var slidelist = ''; if (settings.dynamic == true) { for (var i = 0; i < settings.dynamicel.length; i++) { slidelist += '
'; } } else { $children.each(function () { slidelist += '
'; }); } $slider.append(slidelist); $slide = $gallery.find('.lightgallery-slide'); }, closeslide: function () { var $this = this; if(settings.closable) { $('.lightgallery-slide') .on('click', function(event) { console.log(event.target); if($(event.target).is('.lightgallery-slide')) { $this.destroy(); } }) ; } $('#lightgallery-close').bind('click touchend', function () { $this.destroy(); }); }, getwidth: function () { var resizewindow = function () { windowwidth = $(window).width(); }; $(window).bind('resize.lightgallery', resizewindow()); }, docss: function () { var support = function () { var transition = ['transition', 'moztransition', 'webkittransition', 'otransition', 'mstransition', 'khtmltransition']; var root = document.documentelement; for (var i = 0; i < transition.length; i++) { if (transition[i] in root.style) { //cssprefix = transition[i].replace('transition', '').tolowercase(); //cssprefix == 'transition' ? cssprefix = 'transition' : cssprefix = ('-'+cssprefix+'-transition'); return true; } } }; if (settings.usecss && support()) { return true; } return false; }, enabletouch: function () { var $this = this; if (istouch){ var startcoords = {}, endcoords = {}; $('body').on('touchstart.lightgallery', function(e) { endcoords = e.originalevent.targettouches[0]; startcoords.pagex = e.originalevent.targettouches[0].pagex; startcoords.pagey = e.originalevent.targettouches[0].pagey; }); $('body').on('touchmove.lightgallery', function(e) { var orig = e.originalevent; endcoords = orig.targettouches[0]; e.preventdefault(); }); $('body').on('touchend.lightgallery', function(e) { var distance = endcoords.pagex - startcoords.pagex, swipethreshold = settings.swipethreshold; if( distance >= swipethreshold ){ $this.prevslide(); clearinterval(interval); } else if( distance <= - swipethreshold ){ $this.nextslide(); clearinterval(interval); } }); } }, touch: function () { var xstart, xend; var $this = this; $('.lightgallery').bind('mousedown', function (e) { e.stoppropagation(); e.preventdefault(); xstart = e.pagex; }); $('.lightgallery').bind('mouseup', function (e) { e.stoppropagation(); e.preventdefault(); xend = e.pagex; if (xend - xstart > 20) { $this.prevslide(); } else if (xstart - xend > 20) { $this.nextslide(); } }); }, isvideo: function (src) { var youtube = src.match(/\/\/(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=|embed\/)?([a-z0-9_\-]+)/i); var vimeo = src.match(/\/\/(?:www\.)?vimeo.com\/([0-9a-z\-_]+)/i); if (youtube || vimeo) { return true; } }, loadvideo: function (src, a, _id) { var youtube = src.match(/\/\/(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=|embed\/)?([a-z0-9_\-]+)/i); var vimeo = src.match(/\/\/(?:www\.)?vimeo.com\/([0-9a-z\-_]+)/i); var video = ''; if (youtube) { if (settings.videoautoplay === true && a === true) { a = '?autoplay=1&rel=0&wmode=opaque'; } else { a = '?wmode=opaque'; } video = ''; } else if (vimeo) { if (settings.videoautoplay === true && a === true) { a = 'autoplay=1&'; } else { a = ''; } video = ''; } return '
' + video + '
'; }, loadcontent: function (index) { var $this = this; var i, j, ob, l = $children.length - index; var src; $this.autostart(); if (settings.mobilesrc === true && windowwidth <= settings.mobilesrcmaxwidth) { if (settings.dynamic == true) { src = settings.dynamicel[0]['mobilesrc']; } else { src = $children.eq(index).attr('data-responsive-src'); } } else { if (settings.dynamic == true) { src = settings.dynamicel[0]['src']; } else { src = $children.eq(index).attr('data-src'); } } if (!$this.isvideo(src)) { $slide.eq(index).prepend(''); ob = $('img'); } else { $slide.eq(index).prepend($this.loadvideo(src, true, index)); ob = $('iframe'); if (settings.auto && settings.videoautoplay === true) { clearinterval(interval); } } if ($children.length > 1) { $slide.eq(index).find(ob).on('load error', function () { for (i = 0; i <= index - 1; i++) { var src; if (settings.mobilesrc === true && windowwidth <= settings.mobilesrcmaxwidth) { if (settings.dynamic == true) { src = settings.dynamicel[index - i - 1]['mobilesrc']; } else { src = $children.eq(index - i - 1).attr('data-responsive-src'); } } else { if (settings.dynamic == true) { src = settings.dynamicel[index - i - 1]['src']; } else { src = $children.eq(index - i - 1).attr('data-src'); } } if (!$this.isvideo(src)) { $slide.eq(index - i - 1).prepend(''); } else { $slide.eq(index - i - 1).prepend($this.loadvideo(src, false, index - i - 1)); } } for (j = 1; j < l; j++) { var src; if (settings.mobilesrc === true && windowwidth <= settings.mobilesrcmaxwidth) { if (settings.dynamic == true) { src = settings.dynamicel[index + j]['mobilesrc']; } else { src = $children.eq(index + j).attr('data-responsive-src'); } } else { if (settings.dynamic == true) { src = settings.dynamicel[index + j]['src']; } else { src = $children.eq(index + j).attr('data-src'); } } if (!$this.isvideo(src)) { $slide.eq(index + j).prepend(''); } else { $slide.eq(index + j).prepend($this.loadvideo(src, false, index + j)); } } }); } }, addcaption: function () { if (settings.caption === true) { var i, title = false; for (i = 0; i < $children.length; i++) { if (settings.dynamic == true) { title = settings.dynamicel[i]['caption']; } else { title = $children.eq(i).attr('data-title'); } if (typeof title == 'undefined' || title == null) { title = 'image ' + i + ''; } if (settings.captionlink === true) { var link = $children.eq(i).attr('data-link'); if (typeof link !== 'undefined' && link !== '') { link = link } else { link = '#' } $slide.eq(i).append('
' + title + '
'); } else { $slide.eq(i).append('
' + title + '
'); } } } }, adddesc: function () { if (settings.desc === true) { var i, description = false; for (i = 0; i < $children.length; i++) { if (settings.dynamic == true) { description = settings.dynamicel[i]['desc']; } else { description = $children.eq(i).attr('data-desc');; } if (typeof description == 'undefined' || description == null) { description = 'image ' + i + ''; } if (settings.caption === false) { $slide.eq(i).append('
' + description + '
'); } else { $slide.eq(i).find('.info').append('' + description + ''); } } } }, counter: function() { if (settings.counter === true) { var slidecount = $("#lightgallery-slider > div").length; $gallery.append("
/ "+slidecount+"
"); } }, buildthumbnail: function () { if (settings.thumbnail === true && $children.length > 1) { var $this = this; $gallery.append('
'); $thumb_cont = $gallery.find('.thumb_cont'); $prev.after(''); $gallery.find('.clthumb').bind('click touchend', function () { $thumb_cont.addclass('open'); if ($this.docss() && settings.mode === 'slide') { $slide.eq(index).prevall().removeclass('nextslide').addclass('prevslide'); $slide.eq(index).nextall().removeclass('prevslide').addclass('nextslide'); } }); $gallery.find('.close').bind('click touchend', function () { $thumb_cont.removeclass('open'); }); var thumbinfo = $gallery.find('.thumb_info'); var $thumb_inner = $gallery.find('.thumb_inner'); var thumblist = ''; var thumbimg; if (settings.dynamic == true) { for (var i = 0; i < settings.dynamicel.length; i++) { thumbimg = settings.dynamicel[i]['thumb']; thumblist += '
'; } } else { $children.each(function () { if (settings.exthumbimage === false || typeof $(this).attr(settings.exthumbimage) == 'undefined' || $(this).attr(settings.exthumbimage) == null) { thumbimg = $(this).find('img').attr('src'); } else { thumbimg = $(this).attr(settings.exthumbimage); } thumblist += '
'; }); } $thumb_inner.append(thumblist); $thumb = $thumb_inner.find('.thumb'); $thumb.bind('click touchend', function () { usingthumb = true; var index = $(this).index(); $thumb.removeclass('active'); $(this).addclass('active'); $this.slide(index); clearinterval(interval); }); thumbinfo.prepend('' + settings.lang.allphotos + ' (' + $thumb.length + ')'); } }, slideto: function () { var $this = this; if (settings.controls === true && $children.length > 1) { $gallery.append('
'); $prev = $gallery.find('#lightgallery-prev'); $next = $gallery.find('#lightgallery-next'); $prev.bind('click', function () { $this.prevslide(); clearinterval(interval); }); $next.bind('click', function () { $this.nextslide(); clearinterval(interval); }); } }, autostart: function () { var $this = this; if (settings.auto === true) { interval = setinterval(function () { if (index + 1 < $children.length) { index = index; } else { index = -1; } index++; $this.slide(index); }, settings.pause); } }, keypress: function () { var $this = this; $(window).bind('keyup.lightgallery', function (e) { e.preventdefault(); e.stoppropagation(); if (e.keycode === 37) { $this.prevslide(); clearinterval(interval); } if (e.keycode === 38 && settings.thumbnail === true) { if (!$thumb_cont.hasclass('open')) { if ($this.docss() && settings.mode === 'slide') { $slide.eq(index).prevall().removeclass('nextslide').addclass('prevslide'); $slide.eq(index).nextall().removeclass('prevslide').addclass('nextslide'); } $thumb_cont.addclass('open'); } } else if (e.keycode === 39) { $this.nextslide(); clearinterval(interval); } if (e.keycode === 40 && settings.thumbnail === true) { if ($thumb_cont.hasclass('open')) { $thumb_cont.removeclass('open'); } } else if (settings.esckey === true && e.keycode === 27) { if (settings.thumbnail === true && $thumb_cont.hasclass('open')) { $thumb_cont.removeclass('open'); } else { $this.destroy(); } } }); }, nextslide: function () { var $this = this; index = $slide.index($slide.eq(previndex)); if (index + 1 < $children.length) { index++; $this.slide(index); } else { if (settings.loop) { index = 0; $this.slide(index); } else if (settings.mode === 'fade' && settings.thumbnail === true && $children.length > 1) { $thumb_cont.addclass('open'); } } settings.onslidenext.call(this); }, prevslide: function () { var $this = this; index = $slide.index($slide.eq(previndex)); if (index > 0) { index--; $this.slide(index); } else { if (settings.loop) { index = $children.length - 1; $this.slide(index); } else if (settings.mode === 'fade' && settings.thumbnail === true && $children.length > 1) { $thumb_cont.addclass('open'); } } settings.onslideprev.call(this); }, slide: function (index) { if (lightgalleryon) { if (!$slider.hasclass('on')) { $slider.addclass('on'); } if (this.docss() && settings.speed !== '') { if (!$slider.hasclass('speed')) { $slider.addclass('speed'); } if (aspeed === false) { $slider.css('transition-duration', settings.speed + 'ms'); aspeed = true; } } if (this.docss() && settings.easing !== '') { if (!$slider.hasclass('timing')) { $slider.addclass('timing'); } if (atiming === false) { $slider.css('transition-timing-function', settings.easing); atiming = true; } } settings.onslidebefore.call(this); } if (settings.mode === 'slide') { if (this.docss() && !$slider.hasclass('slide')) { $slider.addclass('slide'); } /* if(this.docss()){ $slider.css({ 'transform' : 'translate3d('+(-index*100)+'%, 0px, 0px)' }); }*/ if (!this.docss() && !lightgalleryon) { $slider.css({ left: (-index * 100) + '%' }); //$slide.eq(index).css('transition','none'); } else if (!this.docss() && lightgalleryon) { $slider.animate({ left: (-index * 100) + '%' }, settings.speed, settings.easing); } } else if (settings.mode === 'fade') { if (this.docss() && !$slider.hasclass('fadem')) { $slider.addclass('fadem'); } else if (!this.docss() && !$slider.hasclass('animate')) { $slider.addclass('animate'); } if (!this.docss() && !lightgalleryon) { $slide.fadeout(100); $slide.eq(index).fadein(100); } else if (!this.docss() && lightgalleryon) { $slide.eq(previndex).fadeout(settings.speed, settings.easing); $slide.eq(index).fadein(settings.speed, settings.easing); } } if (index + 1 >= $children.length && settings.auto && settings.loop === false) { clearinterval(interval); } $slide.eq(previndex).removeclass('current'); $slide.eq(index).addclass('current'); if (this.docss() && settings.mode === 'slide') { if (usingthumb === false) { $('.prevslide').removeclass('prevslide'); $('.nextslide').removeclass('nextslide'); $slide.eq(index - 1).addclass('prevslide'); $slide.eq(index + 1).addclass('nextslide'); } else { $slide.eq(index).prevall().removeclass('nextslide').addclass('prevslide'); $slide.eq(index).nextall().removeclass('prevslide').addclass('nextslide'); } } if (settings.thumbnail === true && $children.length > 1) { $thumb.removeclass('active'); $thumb.eq(index).addclass('active'); } if (settings.controls && settings.hidecontrolonend && settings.loop === false) { if (index === 0) { $prev.addclass('disabled'); } else if (index === $children.length - 1) { $next.addclass('disabled'); } else { $prev.add($next).removeclass('disabled'); } } previndex = index; lightgalleryon === false ? settings.onopen.call(this) : settings.onslideafter.call(this); lightgalleryon = true; usingthumb = false; if (settings.counter) { $("#lightgallery_counter_current").text(index+1); } }, destroy: function () { settings.onbeforeclose.call(this); lightgalleryon = false; atiming = false; aspeed = false; usingthumb = false; clearinterval(interval); $('.lightgallery').off('mousedown mouseup'); $('body').off('touchstart.lightgallery touchmove.lightgallery touchend.lightgallery'); $(window).off('resize.lightgallery keyup.lightgallery'); $gallery.addclass('fadem'); settimeout(function () { $gallerycont.remove(); $('body').removeclass('lightgallery'); }, 500); settings.oncloseafter.call(this); } }; lightgallery.init(); return this; }; }(jquery));