diff --git a/lightbox/lightbox.js b/lightbox/lightbox.js index 1c6af73..9853f22 100644 --- a/lightbox/lightbox.js +++ b/lightbox/lightbox.js @@ -160,36 +160,24 @@ }); }); - body.addEventListener('click', function (event) { - if (body.contains(container)) { - var target = event.target; - if (target === container || target === screenCover || target === btnClose) { - closeLightbox(); - } - if (target === btnNav.next) { - galleryNavigation('next'); - } - if (target === btnNav.previous) { - galleryNavigation('previous'); - } - } - }); - - body.addEventListener('keyup', function (event) { - if (body.contains(container)) { - var key = event.keyCode; - if (key === 27) { - closeLightbox(); - } - if (container.classList.contains('lightbox-gallery')) { - if (key === 39) { - galleryNavigation('next'); + Array.prototype.forEach.call(['click', 'keyup'], function (event) { + body.addEventListener(event, function (event) { + if (body.contains(container)) { + var target = event.target, + key = event.keyCode; + if ([container, screenCover, btnClose].indexOf(target) !== -1 || key === 27) { + closeLightbox(); } - if (key === 37) { - galleryNavigation('previous'); + if (container.classList.contains('lightbox-gallery')) { + if (target === btnNav.next || key === 39) { + galleryNavigation('next'); + } + if (target === btnNav.previous || key === 37) { + galleryNavigation('previous'); + } } } - } + }); }); }()); diff --git a/lightbox/lightbox.min.js b/lightbox/lightbox.min.js index 7e2d1d0..99a3ac6 100644 --- a/lightbox/lightbox.min.js +++ b/lightbox/lightbox.min.js @@ -1 +1 @@ -!function(){"use strict";function e(e){var t,a,i=e.getAttribute("href");return i.match(/\.(jpeg|jpg|gif|png)/)?(t=document.createElement("img"),t.className="lightbox-image",t.src=i,t.alt=e.getAttribute("data-image-alt"),t):i.match(/(youtube|vimeo)/)?(a=[],i.match("youtube")&&(a.id=i.split(/v\/|v=|youtu\.be\//)[1].split(/[?&]/)[0],a.url="www.youtube.com/embed/",a.options="?autoplay=1&rel=0"),i.match("vimeo")&&(a.id=i.split(/video\/|https:\/\/vimeo\.com\//)[1].split(/[?&]/)[0],a.url="player.vimeo.com/video/",a.options="?autoplay=1title=0&byline=0&portrait=0"),a.player=document.createElement("iframe"),a.player.setAttribute("allowfullscreen",""),a.player.className="lightbox-video-player",a.player.src="https://"+a.url+a.id+a.options,a.wrapper=document.createElement("div"),a.wrapper.className="lightbox-video-wrapper",a.wrapper.appendChild(a.player),a.wrapper):o.querySelector(i).children[0].cloneNode(!0)}function t(e){var t,a={next:e.parentElement.nextElementSibling,previous:e.parentElement.previousElementSibling};for(t in a)null!==a[t]&&(a[t]=a[t].querySelector("[data-lightbox]"));return a}function a(a){if(a.blur(),c=a,a.classList.add("current-lightbox-item"),r=document.createElement("button"),r.className="lightbox-btn lightbox-btn-close",m=document.createElement("div"),m.className="screen-cover",m.style.animation=n.fadeIn,u=m.cloneNode(!1),u.className="lightbox-content",u.appendChild(e(a)),p=m.cloneNode(!1),p.className="lightbox-wrapper",p.style.animation=[n.scaleIn,n.fadeIn],p.appendChild(u),p.appendChild(r),d=m.cloneNode(!1),d.className="lightbox-container",d.appendChild(p),"gallery"===a.getAttribute("data-lightbox")){d.classList.add("lightbox-gallery");var i;s={next:"",previous:""};for(i in s)s.hasOwnProperty(i)&&(s[i]=r.cloneNode(!1),s[i].className="lightbox-btn lightbox-btn-"+i,s[i].disabled=null===t(a)[i],p.appendChild(s[i]))}o.style.overflow="hidden",o.appendChild(m),o.appendChild(d)}function i(a){p.removeAttribute("style");var i,l=t(c)[a];if(null!==l){u.style.animation=n.fadeOut,setTimeout(function(){u.replaceChild(e(l),u.children[0]),u.style.animation=n.fadeIn},200),c.classList.remove("current-lightbox-item"),l.classList.add("current-lightbox-item"),c=l;for(i in s)s.hasOwnProperty(i)&&(s[i].disabled=null===t(l)[i])}}function l(){m.style.animation=n.fadeOut,p.style.animation=[n.scaleOut,n.fadeOut],setTimeout(function(){o.contains(m)&&(o.removeAttribute("style"),o.removeChild(m),o.removeChild(d),c.focus(),c.classList.remove("current-lightbox-item"))},200)}var n,o,r,s,c,d,u,p,m,h;h=(o=document.body).querySelectorAll("[data-lightbox]"),n={fadeIn:"fadeIn .3s",fadeOut:"fadeOut .3s",scaleIn:"createBox .3s",scaleOut:"deleteBox .3s"},Array.prototype.forEach.call(h,function(e){e.addEventListener("click",function(t){t.preventDefault(),a(e)})}),o.addEventListener("click",function(e){if(o.contains(d)){var t=e.target;t!==d&&t!==m&&t!==r||l(),t===s.next&&i("next"),t===s.previous&&i("previous")}}),o.addEventListener("keyup",function(e){if(o.contains(d)){var t=e.keyCode;27===t&&l(),d.classList.contains("lightbox-gallery")&&(39===t&&i("next"),37===t&&i("previous"))}})}(); \ No newline at end of file +!function(){"use strict";function e(e){var t,l,a=e.getAttribute("href");return a.match(/\.(jpeg|jpg|gif|png)/)?(t=document.createElement("img"),t.className="lightbox-image",t.src=a,t.alt=e.getAttribute("data-image-alt"),t):a.match(/(youtube|vimeo)/)?(l=[],a.match("youtube")&&(l.id=a.split(/v\/|v=|youtu\.be\//)[1].split(/[?&]/)[0],l.url="www.youtube.com/embed/",l.options="?autoplay=1&rel=0"),a.match("vimeo")&&(l.id=a.split(/video\/|https:\/\/vimeo\.com\//)[1].split(/[?&]/)[0],l.url="player.vimeo.com/video/",l.options="?autoplay=1title=0&byline=0&portrait=0"),l.player=document.createElement("iframe"),l.player.setAttribute("allowfullscreen",""),l.player.className="lightbox-video-player",l.player.src="https://"+l.url+l.id+l.options,l.wrapper=document.createElement("div"),l.wrapper.className="lightbox-video-wrapper",l.wrapper.appendChild(l.player),l.wrapper):o.querySelector(a).children[0].cloneNode(!0)}function t(e){var t,l={next:e.parentElement.nextElementSibling,previous:e.parentElement.previousElementSibling};for(t in l)null!==l[t]&&(l[t]=l[t].querySelector("[data-lightbox]"));return l}function l(l){if(l.blur(),c=l,l.classList.add("current-lightbox-item"),r=document.createElement("button"),r.className="lightbox-btn lightbox-btn-close",m=document.createElement("div"),m.className="screen-cover",m.style.animation=n.fadeIn,u=m.cloneNode(!1),u.className="lightbox-content",u.appendChild(e(l)),p=m.cloneNode(!1),p.className="lightbox-wrapper",p.style.animation=[n.scaleIn,n.fadeIn],p.appendChild(u),p.appendChild(r),d=m.cloneNode(!1),d.className="lightbox-container",d.appendChild(p),"gallery"===l.getAttribute("data-lightbox")){d.classList.add("lightbox-gallery");var a;s={next:"",previous:""};for(a in s)s.hasOwnProperty(a)&&(s[a]=r.cloneNode(!1),s[a].className="lightbox-btn lightbox-btn-"+a,s[a].disabled=null===t(l)[a],p.appendChild(s[a]))}o.style.overflow="hidden",o.appendChild(m),o.appendChild(d)}function a(l){p.removeAttribute("style");var a,i=t(c)[l];if(null!==i){u.style.animation=n.fadeOut,setTimeout(function(){u.replaceChild(e(i),u.children[0]),u.style.animation=n.fadeIn},200),c.classList.remove("current-lightbox-item"),i.classList.add("current-lightbox-item"),c=i;for(a in s)s.hasOwnProperty(a)&&(s[a].disabled=null===t(i)[a])}}function i(){m.style.animation=n.fadeOut,p.style.animation=[n.scaleOut,n.fadeOut],setTimeout(function(){o.contains(m)&&(o.removeAttribute("style"),o.removeChild(m),o.removeChild(d),c.focus(),c.classList.remove("current-lightbox-item"))},200)}var n,o,r,s,c,d,u,p,m,h;h=(o=document.body).querySelectorAll("[data-lightbox]"),n={fadeIn:"fadeIn .3s",fadeOut:"fadeOut .3s",scaleIn:"createBox .3s",scaleOut:"deleteBox .3s"},Array.prototype.forEach.call(h,function(e){e.addEventListener("click",function(t){t.preventDefault(),l(e)})}),Array.prototype.forEach.call(["click","keyup"],function(e){o.addEventListener(e,function(e){if(o.contains(d)){var t=e.target,l=e.keyCode;-1===[d,m,r].indexOf(t)&&27!==l||i(),d.classList.contains("lightbox-gallery")&&(t!==s.next&&39!==l||a("next"),t!==s.previous&&37!==l||a("previous"))}})})}(); \ No newline at end of file