ES6 snippet - JavaScript - scroll to top snippet - window - scrolltotop btn

PHOTO EMBED

Wed Nov 04 2020 08:14:40 GMT+0000 (Coordinated Universal Time)

Saved by @julien #javascript

const scrollToTop = () => {
  const c = document.documentElement.scrollTop || document.body.scrollTop;
  if (c > 0) {
    window.requestAnimationFrame(scrollToTop);
    window.scrollTo(0, c - c / 8);
  }
};

// Example
scrollToTop();
content_copyCOPY

https://medium.com/swlh/24-modern-es6-code-snippets-to-solve-practical-js-problems-3609f301859e