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