@loveholidays/keen-slider
Version:
The HTML touch slider carousel with the most native feeling you will get.
12 lines (10 loc) • 9.53 kB
JavaScript
/**
* keen-slider 5.2.2
* The HTML touch slider carousel with the most native feeling you will get.
* https://keen-slider.io
* Copyright 2020-2020 Eric Beyer <contact@ericbeyer.de>
* License: MIT
* Released on: 2020-07-28
*/
function t(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}function n(t,n){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);n&&(r=r.filter((function(n){return Object.getOwnPropertyDescriptor(t,n).enumerable}))),e.push.apply(e,r)}return e}function e(e){for(var r=1;r<arguments.length;r++){var i=null!=arguments[r]?arguments[r]:{};r%2?n(Object(i),!0).forEach((function(n){t(e,n,i[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):n(Object(i)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))}))}return e}function r(t){return function(t){if(Array.isArray(t))return i(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||function(t,n){if(!t)return;if("string"==typeof t)return i(t,n);var e=Object.prototype.toString.call(t).slice(8,-1);"Object"===e&&t.constructor&&(e=t.constructor.name);if("Map"===e||"Set"===e)return Array.from(t);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return i(t,n)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=new Array(n);e<n;e++)r[e]=t[e];return r}function o(t){return Array.prototype.slice.call(t)}function a(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document;return"function"==typeof t?o(t()):"string"==typeof t?o(n.querySelectorAll(t)):t instanceof HTMLElement!=!1?[t]:t instanceof NodeList!=!1?t:[]}function u(t,n,e){return Math.min(Math.max(t,n),e)}Math.sign||(Math.sign=function(t){return(t>0)-(t<0)||+t});export default function(t){var n,i,o,c,f,s,l,d,h,v,p,m,b,g,w,y,M,O,S,A,j,E,k,x,P,T,D,C,L,X,Y,z,H=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},I="data-keen-slider-moves",V="data-keen-slider-v",W=[],q=null,F=!1,_=!1,N=0,R=[];function U(t,n,e){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};t.addEventListener(n,e,r),W.push([t,n,e,r])}function $(t){if(O&&S===K(t)&&ut()){var e=Z(t).x;if(!nt(t)&&x)return G(t);x&&(qt(),A=e,n.setAttribute(I,!0),x=!1),t.cancelable&&t.preventDefault(),Lt(k(A-e,$t),t.timeStamp),A=e}}function B(t){O||!ut()||tt(t.target)||(O=!0,x=!0,S=K(t),nt(t),dt(),M=v,A=Z(t).x,Lt(0,t.timeStamp),ot("dragStart"))}function G(t){O&&S===K(t,!0)&&ut()&&(n.removeAttribute(I),O=!1,pt(),ot("dragEnd"))}function J(t){return t.changedTouches}function K(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],e=n?J(t):Q(t);return e?e[0]?e[0].identifier:"error":"default"}function Q(t){return t.targetTouches}function Z(t){var n=Q(t);return{x:st()?n?n[0].screenY:t.pageY:n?n[0].screenX:t.pageX,timestamp:t.timeStamp}}function tt(t){return t.hasAttribute(y.preventEvent)}function nt(t){var n=Q(t);if(!n)return!0;var e=n[0],r=st()?e.clientY:e.clientX,i=st()?e.clientX:e.clientY,o=void 0!==j&&void 0!==E&&Math.abs(E-i)<=Math.abs(j-r);return j=r,E=i,o}function et(t){ut()&&O&&t.preventDefault()}function rt(){U(window,"orientationchange",xt),U(window,"resize",(function(){return kt()})),U(n,"mousemove",$),U(n,"mouseleave",G),U(n,"mouseup",G),U(n,"mousedown",B,{passive:!0}),U(n,"touchstart",B,{passive:!0}),U(n,"touchend",G,{passive:!0}),U(n,"touchcancel",G,{passive:!0}),U(n,"touchmove",$,{passive:!1}),H.deactivateWheelEvent&&U(window,"wheel",et,{passive:!1})}function it(){W.forEach((function(t){t[0].removeEventListener(t[1],t[2],t[3])})),W=[]}function ot(t){y[t]&&y[t]($t)}function at(){return y.centered}function ut(){return void 0!==i?i:y.controls}function ct(){return y.loop}function ft(){return!y.loop&&y.rubberband}function st(){return!!y.vertical}function lt(){P=window.requestAnimationFrame(ht)}function dt(){P&&(window.cancelAnimationFrame(P),P=null),T=null}function ht(t){T||(T=t);var n=t-T,e=vt(n);if(n>=C)return Lt(D-X,!1),z?z():void ot("afterChange");var r=Xt(e);if(0===r||ct()||ft()||Y){if(0!==r&&ft()&&!Y)return yt();X+=e,Lt(e,!1),lt()}else Lt(e-r,!1)}function vt(t){return D*L(t/C)-X}function pt(){switch(ot("beforeChange"),y.mode){case"free":gt();break;case"free-snap":wt();break;case"snap":default:mt()}}function mt(){bt((1===l&&0!==p?M:v)+Math.sign(p))}function bt(t,n){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:y.duration,r=arguments.length>3&&void 0!==arguments[3]&&arguments[3],i=arguments.length>4&&void 0!==arguments[4]&&arguments[4],o=function(t){return 1+--t*t*t*t*t};Mt(It(t=Ht(t,r,i)),e,o,n)}function gt(){if(0===b)return!(!Xt(0)||ct())&&bt(v);var t=y.friction/Math.pow(Math.abs(b),-.5);Mt(Math.pow(b,2)/t*Math.sign(b),6*Math.abs(b/t),(function(t){return 1-Math.pow(1-t,5)}))}function wt(){if(0===b)return bt(v);var t=y.friction/Math.pow(Math.abs(b),-.5),n=Math.pow(b,2)/t*Math.sign(b),e=6*Math.abs(b/t),r=(N+n)/(s/l);Mt((-1===p?Math.floor(r):Math.ceil(r))*(s/l)-N,e,(function(t){return 1-Math.pow(1-t,5)}))}function yt(){if(dt(),0===b)return bt(v,!0);var t=.04/Math.pow(Math.abs(b),-.5),n=Math.pow(b,2)/t*Math.sign(b),e=function(t){return--t*t*t+1},r=b;Mt(n,3*Math.abs(r/t),e,!0,(function(){Mt(It(Ht(v)),500,e,!0)}))}function Mt(t,n,e,r,i){dt(),D=t,X=0,C=n,L=e,Y=r,z=i,T=null,lt()}function Ot(e){var r=a(t);r.length&&(n=r[0],kt(e),rt(),ot("mounted"))}function St(){var t,n=H.breakpoints||[];for(var r in n)window.matchMedia(r).matches&&(t=r);if(t===q)return!0;var i=(q=t)?n[q]:H;i.breakpoints&&q&&delete i.breakpoints,y=e(e(e({},Ut),H),i),F=!0,h=null,Et()}function At(t){return"function"==typeof t?t():u(t,1,Math.max(ct()?o-1:o,1))}function jt(){St(),_=!0,ot("created")}function Et(t,n){t&&(H=t),n&&(q=null),Pt(),Ot(n)}function kt(t){var e=window.innerWidth;if(St()&&(e!==h||t)){h=e;var r=y.slides;"number"==typeof r?(f=null,o=r):(f=a(r,n),o=f?f.length:0);var i=y.dragSpeed;k="function"==typeof i?i:function(t){return t*i},s=st()?n.offsetHeight:n.offsetWidth,l=At(y.slidesPerView),d=u(y.spacing,0,s/(l-1)-1),s+=d,c=at()?(s/2-s/l/2)/s:0,Dt();var p=!_||F&&y.resetSlide?y.initial:v;Rt(ct()?p:Yt(p)),st()&&n.setAttribute(V,!0),F=!1}}function xt(t){kt(),setTimeout(kt,500),setTimeout(kt,2e3)}function Pt(){it(),Ct(),n&&n.hasAttribute(V)&&n.removeAttribute(V),ot("destroyed")}function Tt(){f&&f.forEach((function(t,n){var e=g[n].distance*s-n*(s/l-d/l-d/l*(l-1)),r=st()?0:e,i=st()?e:0,o="translate3d(".concat(r,"px, ").concat(i,"px, 0)");t.style.transform=o,t.style["-webkit-transform"]=o}))}function Dt(){f&&f.forEach((function(t){var n="calc(".concat(100/l,"% - ").concat(d/l*(l-1),"px)");st()?(t.style["min-height"]=n,t.style["max-height"]=n):(t.style["min-width"]=n,t.style["max-width"]=n)}))}function Ct(){if(f){var t=["transform","-webkit-transform"];t=[].concat(r(t),st?["min-height","max-height"]:["min-width","max-width"]),f.forEach((function(n){t.forEach((function(t){n.style.removeProperty(t)}))}))}}function Lt(t){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Date.now();Wt(t,e),n&&(t=_t(t)),N+=t,Ft()}function Xt(t){var n=s*(o-1*(at()?1:l))/l,e=N+t;return e>n?e-n:e<0?e:0}function Yt(t){return u(t,0,o-1-(at()?0:l-1))}function zt(){var t=Math.abs(w),n=N<0?1-t:t;return{direction:p,progressTrack:n,progressSlides:n*o/(o-1),positions:g,position:N,speed:b,relativeSlide:(v%o+o)%o,absoluteSlide:v,size:o,slidesPerView:l,widthOrHeight:s}}function Ht(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],e=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return ct()?n?Vt(t,e):t:Yt(t)}function It(t){return-(-s/l*t+N)}function Vt(t,n){var e=(v%o+o)%o,r=e<(t=(t%o+o)%o)?-e-o+t:-(e-t),i=e>t?o-e+t:t-e,a=n?Math.abs(r)<=i?r:i:t<e?r:i;return v+a}function Wt(t,n){clearTimeout(m);var e=Math.sign(t);if(e!==p&&qt(),p=e,R.push({distance:t,time:n}),m=setTimeout((function(){R=[],b=0}),50),(R=R.slice(-6)).length<=1||0===p)return b=0;var r=R.slice(0,-1).reduce((function(t,n){return t+n.distance}),0),i=R[R.length-1].time,o=R[0].time;b=u(r/(i-o),-10,10)}function qt(){R=[]}function Ft(){w=ct()?N%(s*o/l)/(s*o/l):N/(s*o/l),Nt();for(var t=[],n=0;n<o;n++){var e=(1/o*n-(w<0&&ct()?w+1:w))*o/l+c;ct()&&(e+=e>(o-1)/l?-o/l:e<-o/l+1?o/l:0);var r=1/l,i=e+r,a=i<r?i/r:i>1?1-(i-1)*l/1:1;t.push({portion:a<0||a>1?0:a,distance:e})}g=t,Tt(),ot("move")}function _t(t){if(ct())return t;var n=Xt(t);if(!ft())return t-n;if(0===n)return t;var e;return t*(e=n/s,(1-Math.abs(e))*(1-Math.abs(e)))}function Nt(){var t=Math.round(N/(s/l));t!==v&&(v=t,ot("slideChanged"))}function Rt(t){ot("beforeChange"),Lt(It(t),!1),ot("afterChange")}var Ut={centered:!1,breakpoints:null,controls:!0,dragSpeed:1,friction:.0025,loop:!1,initial:0,duration:500,preventEvent:"data-keen-slider-pe",slides:".keen-slider__slide",vertical:!1,resetSlide:!1,slidesPerView:1,spacing:0,mode:"snap",rubberband:!0},$t={controls:function(t){i=t},destroy:Pt,refresh:function(t){Et(t,!0)},next:function(){bt(v+1,!0)},prev:function(){bt(v-1,!0)},moveToSlide:function(t,n){bt(t,!0,n)},moveToSlideRelative:function(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],e=arguments.length>2?arguments[2]:void 0;bt(t,!0,e,!0,n)},resize:function(){kt(!0)},details:function(){return zt()}};return jt(),$t}
//# sourceMappingURL=keen-slider.esm.js.map