@m775/touchslide 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/touchslide.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name" : "@m775/touchslide",
|
|
3
|
-
"version" : "1.0.
|
|
3
|
+
"version" : "1.0.6",
|
|
4
4
|
"description": "A lightweight JavaScript touch slider plugin with mobile gesture support, autoplay, infinite loop, navigation and responsive animation.",
|
|
5
5
|
"main" : "touchslide.js",
|
|
6
6
|
"exports": {
|
package/touchslide.js
CHANGED
|
@@ -15,4 +15,4 @@
|
|
|
15
15
|
* 3. Remove copyright notices.
|
|
16
16
|
* ============================================================================
|
|
17
17
|
*/
|
|
18
|
-
(()=>{const t=new WeakSet;class e{#t;#e;#n;constructor(t){this.#t={target:null,contentClass:".slider",navClass:".nav",navOnClass:"on",autoPage:false,touch:true,pagingIndex:".paging .index",pagingTotal:".paging .total",effect:"left",loop:false,autoPlay:false,delayTime:200,interTime:2500,prevClass:null,nextClass:null,prevStop:null,nextStop:null,startFun:null,endFun:null};this.#e=["left","top"];this.#n=false;this.#t.target=t}nav(t,e="on",n=false){Object.assign(this.#t,{navClass:t||".hd ul",navOnClass:e||"on",autoPage:n});return this}content(t){this.#t.contentClass=t||".bd";return this}prev(t,e="prevStop"){Object.assign(this.#t,{prevClass:t,prevStop:e});return this}next(t,e="nextStop"){Object.assign(this.#t,{nextClass:t,nextStop:e});return this}paging(t,e){if(t)this.#t.pagingIndex=t;if(e)this.#t.pagingTotal=e;return this}loop(){this.#t.loop=true;return this}delay(t=200){this.#t.delayTime=t>0?t:200;return this}auto(t=2500){Object.assign(this.#t,{autoPlay:true,interTime:t>0?t:2500});return this}before(t){this.#t.startFun=typeof t==="function"?t:null;return this}after(t){this.#t.endFun=typeof t==="function"?t:null;return this}left(){this.#t.effect="left";return this}top(){this.#t.effect="top";return this}start(){if(this.#n)throw new Error("Touchslide 已经执行 play(),不能重复调用!");const t={translate:{init:()=>{if(L){C.style.touchAction=c.effect==="left"?"pan-y":"pan-x"}p.style.transitionTimingFunction="cubic-bezier(.25,.8,.25,1)";C.style.userSelect="none";if(c.loop&&
|
|
18
|
+
(()=>{const t=new WeakSet;class e{#t;#e;#n;constructor(t){this.#t={target:null,contentClass:".slider",navClass:".nav",navOnClass:"on",autoPage:false,touch:true,pagingIndex:".paging .index",pagingTotal:".paging .total",effect:"left",loop:false,autoPlay:false,delayTime:200,interTime:2500,prevClass:null,nextClass:null,prevStop:null,nextStop:null,startFun:null,endFun:null};this.#e=["left","top"];this.#n=false;this.#t.target=t}nav(t,e="on",n=false){Object.assign(this.#t,{navClass:t||".hd ul",navOnClass:e||"on",autoPage:n});return this}content(t){this.#t.contentClass=t||".bd";return this}prev(t,e="prevStop"){Object.assign(this.#t,{prevClass:t,prevStop:e});return this}next(t,e="nextStop"){Object.assign(this.#t,{nextClass:t,nextStop:e});return this}paging(t,e){if(t)this.#t.pagingIndex=t;if(e)this.#t.pagingTotal=e;return this}loop(){this.#t.loop=true;return this}delay(t=200){this.#t.delayTime=t>0?t:200;return this}auto(t=2500){Object.assign(this.#t,{autoPlay:true,interTime:t>0?t:2500});return this}before(t){this.#t.startFun=typeof t==="function"?t:null;return this}after(t){this.#t.endFun=typeof t==="function"?t:null;return this}left(){this.#t.effect="left";return this}top(){this.#t.effect="top";return this}start(){if(this.#n)throw new Error("Touchslide 已经执行 play(),不能重复调用!");const t={translate:{init:()=>{if(L){C.style.touchAction=c.effect==="left"?"pan-y":"pan-x"}p.style.transitionTimingFunction="cubic-bezier(.25,.8,.25,1)";C.style.userSelect="none";if(c.loop&&O>1){p.appendChild(p.children[0].cloneNode(true));p.insertBefore(p.children[O-1].cloneNode(true),p.children[0]);P+=2}for(const t of p.children){if(c.effect==="left"){t.style.display="table-cell";t.style.verticalAlign="top";t.style.width=G+"px"}else{t.style.display="block";t.style.width=G+"px"}}d.updateSize();const t=()=>{if(_)cancelAnimationFrame(_);_=requestAnimationFrame(d.updateSize)};window.addEventListener("resize",t);if(typeof ResizeObserver==="function"){U=new ResizeObserver(t);U.observe(C)}},animation:(t,e=0)=>{p.style.willChange="transform";if(e>0&&typeof c.startFun==="function")c.startFun(F+1,O);p.style.transitionDuration=`${e}ms`;p._duration=e;p.style.transform=c.effect==="left"?`translate3d(${t}px,0,0)`:`translate3d(0,${t}px,0)`;if(e>0){let t=false;const e=n=>{if(n.target!==p||t)return;t=true;p.removeEventListener("transitionend",e);if(typeof c.endFun==="function")c.endFun(F+1,O);p.style.willChange="auto"};p.addEventListener("transitionend",e)}},updateSize:()=>{N=C.clientWidth;j=C.clientHeight;G=c.effect==="left"?N:j;if(c.effect==="left"){G=N;p.style.width=P*G+"px"}else{G=j;p.style.height=P*G+"px"}const t=c.effect==="left"?"width":"height";for(const e of p.children)e.style[t]=G+"px";d.animation(-M*G,0)},fixLoop:()=>{const t=e=>{if(e.target!==p)return;p.removeEventListener("transitionend",t);p.style.transitionDuration="0ms";p._duration=0;requestAnimationFrame(()=>{d.animation(-M*G,0)})};p.addEventListener("transitionend",t)},touch:t=>{if(L){if(!t.isPrimary)return;if(t.pointerType==="mouse"&&t.button!==0)return;if(p.setPointerCapture)p.setPointerCapture(t.pointerId);if(S)p.addEventListener(S,d.tEnd,{once:true})}p.style.willChange="transform";C.style.cursor="grabbing";l();p.style.transitionDuration="0ms";p._duration=0;z=k=undefined;$=B=0;if(R){cancelAnimationFrame(R);R=null}q=t.clientX;D=t.clientY;H=Date.now();T.addEventListener(x,d.tMove,{passive:false});T.addEventListener(b,d.tEnd)},tMove:t=>{n(t);if(z===undefined){if(Math.abs($)>5||Math.abs(B)>5){z=Math.abs($)>Math.abs(B);k=!z}}if(c.effect==="left"?!k:!z){t.preventDefault();let e=c.effect==="left"?-M*G+$*(!c.loop&&(F===0&&$>0||F>=O-1&&$<0)?.4:1):-M*G+B*(!c.loop&&(F===0&&B>0||F>=O-1&&B<0)?.4:1);if(!R){R=requestAnimationFrame(()=>{R=null;p.style.transform=c.effect==="left"?`translate3d(${e}px,0,0)`:`translate3d(0,${e}px,0)`})}}},tEnd:t=>{const e=S&&t.type===S;if(!e)n(t);if(e){if(R){cancelAnimationFrame(R);R=null}i(t);d.animation(-M*G,c.delayTime);r();return}if(Math.abs($)<3&&Math.abs(B)<3){i(t);r();return}W=true;setTimeout(()=>{W=false},0);if(R){cancelAnimationFrame(R);R=null}let s=false;const o=Date.now()-H;if(c.effect==="left"&&!k){const t=o<300&&Math.abs($)>20;const e=Math.abs($)>Math.max(30,N/10);if(t||e){F+=$>0?-1:1;s=true}}else if(c.effect==="top"&&!z){const t=o<300&&Math.abs(B)>20;const e=Math.abs(B)>j/10;if(t||e){F+=B>0?-1:1;s=true}}if(!s){d.animation(-M*G,c.delayTime)}else{requestAnimationFrame(()=>{requestAnimationFrame(()=>{u()})})}i(t);r()},play:(t=false)=>{if(c.loop){M=F+1;d.animation(-M*G,t?0:c.delayTime);if(F<0){F=O-1;M=O;d.fixLoop()}else if(F>=O){F=0;M=1;d.fixLoop()}}else{F=Math.max(0,Math.min(F,O-1));M=F;d.animation(-M*G,c.delayTime)}}}};const e=()=>{if(g&&X!==F){if(X>=0&&E[X])E[X].classList.remove(c.navOnClass);if(E[F])E[F].classList.add(c.navOnClass);X=F}if(y)y.textContent=(F+1).toString();if(h)c.loop||F>0?h.classList.remove(c.prevStop):h.classList.add(c.prevStop);if(m)c.loop||F<O-1?m.classList.remove(c.nextStop):m.classList.add(c.nextStop)};const n=t=>{$=t.clientX-q;B=t.clientY-D};const i=t=>{if(L){if(t.pointerId!==undefined&&p.hasPointerCapture(t.pointerId)){p.releasePointerCapture(t.pointerId)}if(S)p.removeEventListener(S,d.tEnd)}T.removeEventListener(x,d.tMove);T.removeEventListener(b,d.tEnd);p.style.willChange="";C.style.cursor="grab"};const s=()=>{l();if(c.loop||F<O-1){F++;u()}r()};const o=()=>{l();if(c.loop||F>0){F--;u()}r()};const a=()=>{document.hidden?l():f()};const l=()=>{clearTimeout(A);A=null};const r=()=>{if(!c.loop&&!I){l();I=true}else{f()}};const f=()=>{l();if(!I){if(c.loop||F<O-1){A=setTimeout(()=>{F++;u();f()},c.interTime)}else{I=true}}};const u=(t=false)=>{d.play(t);e()};const c=this.#t;const d=this.#e.includes(c.effect)?t["translate"]:t[c.effect];if(!d)return false;if(!c.target)return false;const p=c.target.querySelector(c.contentClass);if(!p)return false;this.#n=true;const h=c.prevClass?c.target.querySelector(c.prevClass):null;const m=c.nextClass?c.target.querySelector(c.nextClass):null;const y=c.pagingIndex?c.target.querySelector(c.pagingIndex):null;const v=c.pagingTotal?c.target.querySelector(c.pagingTotal):null;let g=c.navClass?c.target.querySelector(c.navClass):null;let E=null;const C=document.createElement("div");Array.from(p.attributes).forEach(t=>{C.setAttribute(t.name,t.value);p.removeAttribute(t.name)});C.style.overflow="hidden";C.style.position="relative";p.style.margin=p.style.padding="0";p.parentNode.insertBefore(C,p);p.style.willChange="transform";p._duration=-1;C.appendChild(p);C.style.overscrollBehavior="none";const L="PointerEvent"in window;const w=L?"pointerdown":"mousedown";const x=L?"pointermove":"mousemove";const b=L?"pointerup":"mouseup";const S=L?"pointercancel":null;const T=L?p:document;if(c.touch&&typeof d.touch=="function"){p.addEventListener(w,d.touch,{passive:true});C.style.cursor="grab";p.addEventListener("dragstart",t=>t.preventDefault());p.addEventListener("click",t=>{if(!W)return;W=false;t.preventDefault();t.stopPropagation()},true)}let F=0;let M=c.loop?1:0;let A=null;let I=!c.autoPlay;let O=p.children.length;let P=O;let q=0,D=0,$=0,B=0,z,k;let N,j,G;let H=0;let _;let U;let R;let W=false;let X=-1;if(c.loop&&O<=1){c.autoPlay=false;c.loop=false;I=true}if(g){if(c.autoPage===true){const t=["OL","UL"].includes(g.tagName)?"li":"span";g.innerHTML=Array.from({length:O},(e,n)=>`<${t} data-index="${n}">${n+1}</${t}>`).join("")}E=g.children;if(E.length){for(let t=0;t<E.length;t++){E[t].addEventListener("click",()=>{l();F=t;u();f()})}}}if(v)v.textContent=O.toString();document.addEventListener("visibilitychange",a);if(h)h.addEventListener("click",()=>{o()});if(m)m.addEventListener("click",()=>{s()});d.init();u(true);if(!I)f()}run(){this.start()}}if(typeof window!=="undefined"){window.GB=window.GB||{};GB.Touchslide=n=>{const i=typeof n==="string"?document.querySelector(n):n instanceof HTMLElement?n:null;if(i){if(t.has(i)){throw new Error("GB.TOUCHSLIDE.DOM.INITED")}else{t.add(i);return new e(i)}}throw new Error("GB.TOUCHSLIDE.INIT.FAIL")}}})();
|