seamless-scroll-react
Version:
react无缝滚动和单步滚动
3 lines (2 loc) • 4.82 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e,n=require("react"),t=(e=n)&&"object"==typeof e&&"default"in e?e.default:e,r=require("throttle-debounce"),o=function(e,o){var u,i=e.list,c=void 0===i?[]:i,l=e.limitScrollNum,a=void 0===l?3:l,s=e.hover,f=void 0!==s&&s,m=e.wheel,d=void 0!==m&&m,w=e.isRemUnit,h=void 0!==w&&w,v=e.isAutoScroll,g=void 0===v||v,b=e.ease,T=void 0===b?"ease-in":b,p=e.delay,M=void 0===p?0:p,R=e.singleWaitTime,y=void 0===R?1e3:R,A=e.direction,F=void 0===A?"up":A,E=e.singleWidth,q=void 0===E?0:E,S=e.singleHeight,x=void 0===S?0:S,C=e.step,z=void 0===C?1:C,H=e.count,N=void 0===H?-1:H,W=e.children,k=e.wrapperClassName,j=e.wrapperHeight,I=new Array(null!=(u=e.copyNum)?u:1).fill(null),L=n.useRef(null),Y=n.useRef(null),_=n.useRef(null),O=n.useRef(0),P=n.useRef(0),U=n.useRef(null),B=n.useRef(null),D=n.useState(0),G=D[0],J=D[1],K=n.useState(0),Q=K[0],V=K[1],X=n.useRef(G),Z=n.useRef(Q);X.current=G,Z.current=Q;var $=n.useRef(0),ee=n.useMemo((function(){return!!c&&c.length>=a}),[a,c]),ne=n.useMemo((function(){return{width:O.current?O.current+"px":"auto",transform:"translate("+G+"px,"+Q+"px)",transition:"all "+("string"==typeof T?T:"cubic-bezier("+T.x1+","+T.y1+","+T.x2+","+T.y2+")")+" "+M+"ms",overflow:"hidden"}}),[M,T,G,Q]),te=n.useMemo((function(){return"left"===F||"right"===F}),[F]),re=n.useMemo((function(){return f&&g&&ee}),[f,ee,g]),oe=n.useMemo((function(){return te?{float:"left",overflow:"hidden"}:{overflow:"hidden"}}),[te]),ue=n.useMemo((function(){return h?parseInt(globalThis.window.getComputedStyle(globalThis.document.documentElement,null).fontSize):1}),[h]),ie=n.useMemo((function(){return q*ue}),[ue,q]),ce=n.useMemo((function(){return x*ue}),[x,ue]),le=n.useMemo((function(){var e,n=z;return(e=te?ie:ce)>0&&e%n>0&&console.warn("如果设置了单步滚动,step 需是单步大小的约数,否则无法保证单步滚动结束的位置是否准确。~~~~~"),n}),[te,z,ce,ie]),ae=function(e,n,t){U.current=requestAnimationFrame((function(){var r=P.current/2,o=O.current/2;"up"===e?(Math.abs(Z.current)>=r&&(V(0),$.current+=1),V((function(e){return e-n}))):"down"===e?(Z.current>=0&&(V(-1*r),$.current+=1),V((function(e){return e+n}))):"left"===e?(Math.abs(X.current)>=o&&(J(0),$.current+=1),J((function(e){return e-n}))):"right"===e&&(X.current>=0&&(J(-1*o),$.current+=1),J((function(e){return e+n}))),t||(B.current&&clearTimeout(B.current),ce?Math.abs(Z.current)%ce==0?B.current=setTimeout((function(){se()}),y):se():ie&&Math.abs(X.current)%ie<n?B.current=setTimeout((function(){se()}),y):se())}))},se=function(){he(),g&&ee&&$.current!==N?ae(F,le,!1):$.current=0},fe=function(){(function(e){e&&"boolean"!=typeof e&&e.length>=400&&console.warn("数据达到了"+e.length+"条有点多哦~,可能会造成部分老旧浏览器卡顿。")})(c),te?O.current=2*Y.current.offsetWidth+1:P.current=_.current.offsetHeight,se()},me=r.throttle(30,(function(e){he();var n=ce||20;e.deltaY<0&&ae("down",n,!0),e.deltaY>0&&ae("up",n,!0)})),de=function(){se()},we=function(){B.current&&clearTimeout(B.current),he()},he=function(){cancelAnimationFrame(U.current),U.current=null};n.useEffect((function(){g?de():we()}),[g]),n.useEffect((function(){0!==N&&de()}),[N]),n.useEffect((function(){return he(),B.current&&clearTimeout(B.current),ee&&g&&fe(),function(){we()}}),[c]),n.useImperativeHandle(o,(function(){return{onReset:function(){B.current&&clearTimeout(B.current),he(),fe()},onStopMove:function(){we()},onStartMove:function(){de()}}}));var ve=n.useMemo((function(){return t.createElement(t.Fragment,null,t.createElement("div",{ref:Y,style:oe},W),ee?I.map((function(e,n){return t.createElement("div",{key:n,style:oe},W)})):null)}),[c]);return t.createElement("div",{ref:L,className:k,style:{height:j||P.current/2,overflow:"hidden"}},t.createElement("div",d&&f?{ref:_,style:ne,onMouseEnter:function(){re&&we()},onMouseLeave:function(){re&&de()},onWheel:function(e){re&&function(e){me(e)}(e)}}:{ref:_,style:ne,onMouseEnter:function(){re&&we()},onMouseLeave:function(){re&&de()}},ve))},u=n.memo(n.forwardRef(o));globalThis.window.cancelAnimationFrame=function(){return globalThis.window.cancelAnimationFrame||globalThis.window.webkitCancelAnimationFrame||globalThis.window.mozCancelAnimationFrame||globalThis.window.oCancelAnimationFrame||globalThis.window.msCancelAnimationFrame||function(e){return globalThis.window.clearTimeout(e)}}(),globalThis.window.requestAnimationFrame=function(){return globalThis.window.requestAnimationFrame||globalThis.window.webkitRequestAnimationFrame||globalThis.window.mozRequestAnimationFrame||globalThis.window.oRequestAnimationFrame||globalThis.window.msRequestAnimationFrame||function(e){return globalThis.window.setTimeout(e,1e3/60)}}(),exports.default=u;
//# sourceMappingURL=seamless-scroll-react.cjs.production.min.js.map