Go straight to code

const scrollToTop = () = > {
  const c = document.documentElement.scrollTop || document.body.scrollTop;
  if (c > 0) {
    window.requestAnimationFrame(scrollToTop);
    window.scrollTo(0, c - c / 8); }};/ / case
scrollToTop();
Copy the code
  • Tell the browser window. RequestAnimationFrame () – you want to perform an animation, and required the browser until the next redraw calls the specified callback function to update the animation. This method takes as an argument a callback function that is executed before the browser’s next redraw.
  • RequestAnimationFrame: Advantage: It is up to the system to determine the timing of the callback function. If the refresh frequency is 60Hz, the callback function will be executed at each refresh interval without frame loss or stalling.