UNPKG

smooth-scroll-into-view-if-needed

Version:

Ponyfill for smooth scrolling elements into view (if needed!)

2 lines (1 loc) 1.4 kB
import e from"scroll-into-view-if-needed";let o;const t=()=>(o||(o="performance"in window?performance.now.bind(performance):Date.now),o());function l(e){const o=t(),n=Math.min((o-e.startTime)/e.duration,1),r=e.ease(n),s=e.startX+(e.x-e.startX)*r,i=e.startY+(e.y-e.startY)*r;e.method(s,i,n,r),s!==e.x||i!==e.y?requestAnimationFrame((()=>l(e))):e.cb()}function n(e,o,n){let r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:600,s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:e=>1+--e*e*e*e*e,i=arguments.length>5?arguments[5]:void 0,a=arguments.length>6?arguments[6]:void 0;const c=e,d=e.scrollLeft,m=e.scrollTop;l({scrollable:c,method:(o,t,l,n)=>{const r=Math.ceil(o),s=Math.ceil(t);e.scrollLeft=r,e.scrollTop=s,null==a||a({target:e,elapsed:l,value:n,left:r,top:s})},startTime:t(),startX:d,startY:m,x:o,y:n,duration:r,ease:s,cb:i})}const r=e=>e&&!e.behavior||"smooth"===e.behavior;const s=function(o,t){const l=t||{};return r(l)?e(o,{block:l.block,inline:l.inline,scrollMode:l.scrollMode,boundary:l.boundary,skipOverflowHiddenElements:l.skipOverflowHiddenElements,behavior:e=>Promise.all(e.reduce(((e,o)=>{let{el:t,left:r,top:s}=o;const i=t.scrollLeft,a=t.scrollTop;return i===r&&a===s?e:[...e,new Promise((e=>n(t,r,s,l.duration,l.ease,(()=>e({el:t,left:[i,r],top:[a,s]})),l.onScrollChange)))]}),[]))}):Promise.resolve(e(o,t))};export{s as default};//# sourceMappingURL=index.js.map