Files
30-seconds-of-code/snippets/scroll-to-top.md
2017-12-07 12:00:34 +02:00

424 B

Scroll to top

Get distance from top using document.documentElement.scrollTop or document.body.scrollTop. Scroll by a fraction of the distance from top. Use window.requestFrame() to animate the scrolling.

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