0 points

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


dashboard

Wed Nov 04 2020 08:14:40 GMT+0000 (UTC)

Posted 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_copy Copy

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