-
Notifications
You must be signed in to change notification settings - Fork 0
/
goScroll-min.js
1 lines (1 loc) · 1.11 KB
/
goScroll-min.js
1
const W=window,D=W.document,CLASS_NAME="auto-scrolling",SPEED=2,STOP_EVENT="ontouchstart"in W||W.navigator.MaxTouchPoints>0||W.navigator.msMaxTouchPoints>0?"touchstart":"wheel";let scrolling=0;function easeOutQuad(t,o,e,n){return Math.round(-e*(t/=n)*(t-2)+o)}function position(t,o,e){return o.nodeType?t.getBoundingClientRect().top-o.getBoundingClientRect().top+e:t.getBoundingClientRect().top+D.documentElement.scrollTop}function listen(){scrolling=0}export default function(t={}){let o=t.context||W,e=o[t.context?"scrollTop":"pageYOffset"],n=t.to&&1===t.to.nodeType?position(t.to,o,e):t.to||0,i=n-(t.offset||0)-e,l=Date.now(),s=Math.abs(i/(t.speed||2)),c=l+s,a=t.context?t=>{o.scrollTop=t}:t=>{o.scrollTo(0,t)};scrolling=1,D.body.classList.add(CLASS_NAME),o.addEventListener(STOP_EVENT,listen),W.requestAnimationFrame(function r(){let u=Date.now();if(scrolling&&u<=c)return a(easeOutQuad(u-l,e,i,s)),void W.requestAnimationFrame(r);scrolling&&W.requestAnimationFrame(()=>{a(n-(t.offset||0))});"function"==typeof t.callback&&t.callback.call(o,scrolling);o.removeEventListener(STOP_EVENT,listen);D.body.classList.remove(CLASS_NAME)})};