swiper
Version:
Most modern mobile touch slider and framework with hardware accelerated transitions
2 lines • 2.9 kB
JavaScript
import{t as nextTick,m as elementTransitionEnd}from"../shared/utils.min.mjs";class LinearSpline{x;y;lastIndex;binarySearch;constructor(t,r){let e,n,o;this.binarySearch=(t,r)=>{for(n=-1,e=t.length;e-n>1;)o=e+n>>1,t[o]<=r?n=o:e=o;return e},this.x=t,this.y=r,this.lastIndex=t.length-1}interpolate(t){if(!t)return 0;const r=this.binarySearch(this.x,t),e=r-1;return(t-this.x[e])*(this.y[r]-this.y[e])/(this.x[r]-this.x[e])+this.y[e]}}const Controller=({swiper:t,extendParams:r,on:e})=>{function n(){return t.params.controller}function o(){t.controller.control&&t.controller.spline&&(t.controller.spline=void 0,delete t.controller.spline)}r({controller:{control:void 0,inverse:!1,by:"slide"}}),t.controller={control:void 0},e("beforeInit",()=>{const r=n().control;if("undefined"!=typeof window&&("string"==typeof r||r instanceof HTMLElement)){return void("string"==typeof r?[...document.querySelectorAll(r)]:[r]).forEach(r=>{t.controller.control||(t.controller.control=[]);const e=t.controller.control;if(r&&r.swiper)e.push(r.swiper);else if(r){const n=`${t.params.eventsPrefix}init`,o=l=>{const s=l.detail;s&&s[0]&&e.push(s[0]),t.update(),r.removeEventListener(n,o)};r.addEventListener(n,o)}})}t.controller.control=r}),e("update",()=>{o()}),e("resize",()=>{o()}),e("observerUpdate",()=>{o()}),e("setTranslate",(r,e,n)=>{t.controller.control&&(!Array.isArray(t.controller.control)&&t.controller.control.destroyed||t.controller.setTranslate(e,n))}),e("setTransition",(r,e,n)=>{t.controller.control&&(!Array.isArray(t.controller.control)&&t.controller.control.destroyed||t.controller.setTransition(e,n))}),Object.assign(t.controller,{setTranslate:function(r,e){const o=t.controller.control;let l,s;const i=t.constructor;function a(r){if(r.destroyed)return;const e=t.rtlTranslate?-t.translate:t.translate,o=n();"slide"===o.by?(!function(r){t.controller.spline=t.params.loop?new LinearSpline(t.slidesGrid,r.slidesGrid):new LinearSpline(t.snapGrid,r.snapGrid)}(r),s=-t.controller.spline.interpolate(-e)):s=0,s&&"container"!==o.by||(l=(r.maxTranslate()-r.minTranslate())/(t.maxTranslate()-t.minTranslate()),!Number.isNaN(l)&&Number.isFinite(l)||(l=1),s=(e-t.minTranslate())*l+r.minTranslate()),o.inverse&&(s=r.maxTranslate()-s),r.updateProgress(s),r.setTranslate(s,t),r.updateActiveIndex(),r.updateSlidesClasses()}if(Array.isArray(o))for(let t=0;t<o.length;t+=1){const r=o[t];r&&r!==e&&r instanceof i&&a(r)}else o instanceof i&&e!==o&&a(o)},setTransition:function(r,e){const n=t.constructor,o=t.controller.control;function l(e){e.destroyed||(e.setTransition(r,t),0!==r&&(e.transitionStart(),e.params.autoHeight&&nextTick(()=>{e.updateAutoHeight()}),elementTransitionEnd(e.wrapperEl,()=>{o&&e.transitionEnd()})))}if(Array.isArray(o))for(let t=0;t<o.length;t+=1){const r=o[t];r&&r!==e&&r instanceof n&&l(r)}else o instanceof n&&e!==o&&l(o)}})};export{Controller as default};
//# sourceMappingURL=controller.min.mjs.map