forked from feimosi/baguetteBox.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baguetteBox.min.js
7 lines (7 loc) · 6.69 KB
/
baguetteBox.min.js
1
2
3
4
5
6
7
/*!
* baguetteBox.js
* @author feimosi
* @version 1.4.0
* @url https://github.com/feimosi/baguetteBox.js
*/
!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t():e.baguetteBox=t()}(this,function(){function e(e,n){D.transforms=m(),D.svg=v(),o(),t(e,n)}function t(e,t){R=document.querySelectorAll(e),[].forEach.call(R,function(e){t&&t.filter&&(O=t.filter);var n=e.getElementsByTagName("a");n=[].filter.call(n,function(e){return O.test(e.href)});var o=V.length;V.push(n),V[o].options=t,[].forEach.call(V[o],function(e,t){var n=function(e){e.preventDefault?e.preventDefault():e.returnValue=!1,s(o),c(t)};U[e]=n,k(e,"click",n)})})}function n(){[].forEach.call(R,function(){var e=V.length-1;[].forEach.call(V[e],function(e){w(e,"click",U[e])}),V.pop()})}function o(){return(N=C("baguetteBox-overlay"))?(x=C("baguetteBox-slider"),B=C("previous-button"),I=C("next-button"),L=C("close-button"),void 0):(N=E("div"),N.id="baguetteBox-overlay",document.getElementsByTagName("body")[0].appendChild(N),x=E("div"),x.id="baguetteBox-slider",N.appendChild(x),B=E("button"),B.id="previous-button",B.innerHTML=D.svg?H:"<",N.appendChild(B),I=E("button"),I.id="next-button",I.innerHTML=D.svg?M:">",N.appendChild(I),L=E("button"),L.id="close-button",L.innerHTML=D.svg?S:"X",N.appendChild(L),B.className=I.className=L.className="baguetteBox-button",a(),void 0)}function i(e){switch(e.keyCode){case 37:p();break;case 39:g();break;case 27:u()}}function a(){k(N,"click",W),k(B,"click",z),k(I,"click",J),k(L,"click",K),k(N,"touchstart",Q),k(N,"touchmove",Y),k(N,"touchend",Z)}function r(){w(N,"click",W),w(B,"click",z),w(I,"click",J),w(L,"click",K),w(N,"touchstart",Q),w(N,"touchmove",Y),w(N,"touchend",Z)}function s(e){if(q!==e){for(q=e,l(V[e].options);x.firstChild;)x.removeChild(x.firstChild);F.length=0;for(var t,n=0;n<V[e].length;n++)t=E("div"),t.className="full-image",t.id="baguette-img-"+n,F.push(t),x.appendChild(F[n])}}function l(e){e||(e={});for(var t in j)A[t]=j[t],"undefined"!=typeof e[t]&&(A[t]=e[t]);x.style.transition=x.style.webkitTransition="fadeIn"===A.animation?"opacity .4s ease":"slideIn"===A.animation?"":"none","auto"===A.buttons&&("ontouchstart"in window||1===V[q].length)&&(A.buttons=!1),B.style.display=I.style.display=A.buttons?"":"none"}function c(e){"block"!==N.style.display&&(k(document,"keydown",i),X=e,d(X,function(){y(X),b(X)}),h(),N.style.display="block",setTimeout(function(){N.className="visible",A.afterShow&&A.afterShow()},50),A.onChange&&A.onChange(X,F.length))}function u(){"none"!==N.style.display&&(w(document,"keydown",i),N.className="",setTimeout(function(){N.style.display="none",A.afterHide&&A.afterHide()},500))}function d(e,t){var n=F[e];if("undefined"!=typeof n){if(n.getElementsByTagName("img")[0])return t&&t(),void 0;imageElement=V[q][e],imageCaption="function"==typeof A.captions?A.captions.call(V[q],imageElement):imageElement.getAttribute("data-caption")||imageElement.title,imageSrc=f(imageElement);var o=E("figure"),i=E("img"),a=E("figcaption");n.appendChild(o),o.innerHTML='<div class="spinner"><div class="double-bounce1"></div><div class="double-bounce2"></div></div>',i.onload=function(){var n=document.querySelector("#baguette-img-"+e+" .spinner");o.removeChild(n),!A.async&&t&&t()},i.setAttribute("src",imageSrc),o.appendChild(i),A.captions&&imageCaption&&(a.innerHTML=imageCaption,o.appendChild(a)),A.async&&t&&t()}}function f(e){var t=imageElement.href;if(e.dataset){var n=[];for(var o in e.dataset)"at-"!==o.substring(0,3)||isNaN(o.substring(3))||(n[o.replace("at-","")]=e.dataset[o]);keys=Object.keys(n).sort(function(e,t){return parseInt(e)<parseInt(t)?-1:1});for(var i=window.innerWidth*window.devicePixelRatio,a=0;a<keys.length-1&&keys[a]<i;)a++;t=n[keys[a]]||t}return t}function g(){var e;return X<=F.length-2?(X++,h(),y(X),e=!0):A.animation&&(x.className="bounce-from-right",setTimeout(function(){x.className=""},400),e=!1),A.onChange&&A.onChange(X,F.length),e}function p(){var e;return X>=1?(X--,h(),b(X),e=!0):A.animation&&(x.className="bounce-from-left",setTimeout(function(){x.className=""},400),e=!1),A.onChange&&A.onChange(X,F.length),e}function h(){var e=100*-X+"%";"fadeIn"===A.animation?(x.style.opacity=0,setTimeout(function(){D.transforms?x.style.transform=x.style.webkitTransform="translate3d("+e+",0,0)":x.style.left=e,x.style.opacity=1},400)):D.transforms?x.style.transform=x.style.webkitTransform="translate3d("+e+",0,0)":x.style.left=e}function m(){var e=E("div");return"undefined"!=typeof e.style.perspective||"undefined"!=typeof e.style.webkitPerspective}function v(){var e=E("div");return e.innerHTML="<svg/>","http://www.w3.org/2000/svg"==(e.firstChild&&e.firstChild.namespaceURI)}function y(e){e-X>=A.preload||d(e+1,function(){y(e+1)})}function b(e){X-e>=A.preload||d(e-1,function(){b(e-1)})}function k(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent("on"+t,n)}function w(e,t,n){e.removeEventListener?e.removeEventListener(t,n,!1):e.detachEvent("on"+t,n)}function C(e){return document.getElementById(e)}function E(e){return document.createElement(e)}function T(){r(),n(),w(document,"keydown",i),document.getElementsByTagName("body")[0].removeChild(document.getElementById("baguetteBox-overlay")),X=0,q=-1,R.length=0,V.length=0}var N,x,B,I,L,P,H='<svg width="44" height="60"><polyline points="30 10 10 30 30 50" stroke="rgba(255,255,255,0.5)" stroke-width="4"stroke-linecap="butt" fill="none" stroke-linejoin="round"/></svg>',M='<svg width="44" height="60"><polyline points="14 10 34 30 14 50" stroke="rgba(255,255,255,0.5)" stroke-width="4"stroke-linecap="butt" fill="none" stroke-linejoin="round"/></svg>',S='<svg width="30" height="30"><g stroke="rgb(160, 160, 160)" stroke-width="4"><line x1="5" y1="5" x2="25" y2="25"/><line x1="5" y1="25" x2="25" y2="5"/></g></svg>',A={},j={captions:!0,buttons:"auto",async:!1,preload:2,animation:"slideIn",afterShow:null,afterHide:null,onChange:null},D={},X=0,q=-1,G=!1,O=/.+\.(gif|jpe?g|png|webp)/i,R=[],V=[],F=[],U={},W=function(e){e.target&&"IMG"!==e.target.nodeName&&"FIGCAPTION"!==e.target.nodeName&&u()},z=function(e){e.stopPropagation?e.stopPropagation():e.cancelBubble=!0,p()},J=function(e){e.stopPropagation?e.stopPropagation():e.cancelBubble=!0,g()},K=function(e){e.stopPropagation?e.stopPropagation():e.cancelBubble=!0,u()},Q=function(e){P=e.changedTouches[0].pageX},Y=function(e){G||(e.preventDefault?e.preventDefault():e.returnValue=!1,touch=e.touches[0]||e.changedTouches[0],touch.pageX-P>40?(G=!0,p()):touch.pageX-P<-40&&(G=!0,g()))},Z=function(){G=!1};return[].forEach||(Array.prototype.forEach=function(e,t){for(var n=0;n<this.length;n++)e.call(t,this[n],n,this)}),[].filter||(Array.prototype.filter=function(e,t,n,o,i){for(n=this,o=[],i=0;i<n.length;i++)e.call(t,n[i],i,n)&&o.push(n[i]);return o}),{run:e,destroy:T,showNext:g,showPrevious:p}});