-
Notifications
You must be signed in to change notification settings - Fork 4
/
sliding-scroll.min.js
1 lines (1 loc) · 1.49 KB
/
sliding-scroll.min.js
1
const pi=Math.PI,halfPi=Math.PI/2,EASING_FUNCTIONS={linear:o=>o,fastInSlowOut:o=>Math.sin(halfPi*o),slowInFastOut:o=>Math.cos(halfPi*o+pi)+1,slowInSlowOut:o=>.5*(Math.cos(o*pi+pi)+1)};function smoothScrollTo({top:o,left:t,duration:l=0,element:e=document.scrollingElement,easingFunction:s=EASING_FUNCTIONS.slowInSlowOut}){const c=e.scrollTop,a=e.scrollLeft;if(o??=c,t??=a,o=Math.max(0,Math.min(e.scrollHeight-e.clientHeight,o)),t=Math.max(0,Math.min(e.scrollWidth-e.clientWidth,t)),c!==o||a!==t){const u=o-c,m=t-a;let n=0,i=null;function r(o){n+=(o-i)/l,n=Math.min(n,1);var t=s(n);e.scroll({top:c+t*u,left:a+t*m,behavior:"instant"}),1<=n||(i=o,window.requestAnimationFrame(r))}window.requestAnimationFrame(o=>{i=o,window.requestAnimationFrame(r)})}}function smoothScrollToTop({duration:o,easingFunction:t}={}){smoothScrollTo({top:0,duration:o,easingFunction:t})}function smoothScrollToBottom({duration:o,easingFunction:t}={}){var n=document.scrollingElement;smoothScrollTo({top:n.scrollHeight-n.clientHeight,duration:o,easingFunction:t})}function smoothScrollToElement(o,{duration:t,easingFunction:n,offsetTop:i=0,offsetLeft:l=0}={}){o=o.getBoundingClientRect();smoothScrollTo({top:document.scrollingElement.scrollTop+Math.round(o.top)+i,left:document.scrollingElement.scrollLeft+Math.round(o.left)+l,duration:t,easingFunction:n})}function smoothScrollToId(o,{duration:t,easingFunction:n,offsetTop:i=0,offsetLeft:l=0}={}){smoothScrollToElement(document.getElementById(o),{duration:t,easingFunction:n,offsetTop:i,offsetLeft:l})}