import raf from 'raf' ; function easeInOutCubic ( t , b , c , d ) { const cc = c - b ; t /= d / 2 ; if ( t < 1 ) { return ( cc / 2 ) * t * t * t + b ; } return ( cc / 2 ) * ( ( t -= 2 ) * t * t + 2 ) + b ; } function getCurrentScrollTop ( targetNode ) { if ( targetNode === window ) { return window . pageYOffset || document . body . scrollTop || document . documentElement ! . scrollTop ; } return ( targetNode as HTMLElement ) . scrollTop ; } function setScrollTop ( targetNode , value ) { if ( targetNode === window ) { document . body . scrollTop = value ; document . documentElement ! . scrollTop = value ; } else { ( targetNode as HTMLElement ) . scrollTop = value ; } } export default function scrollToTop ( targetNode = window , value = 0 ) { const scrollTop = getCurrentScrollTop ( targetNode ) ; const startTime = Date . now ( ) ; const frameFunc = ( ) => { const timestamp = Date . now ( ) ; const time = timestamp - startTime ; setScrollTop ( targetNode , easeInOutCubic ( time , scrollTop , value , 450 ) ) ; if ( time < 450 ) { raf ( frameFunc ) ; } else { setScrollTop ( targetNode , value ) ; } } ; raf ( frameFunc ) ; }