How to animate window.scrollTo with Javascript NOT jQuery?

I found it, By passing this object, Scroll smoothly :

window.scrollTo({
  top: selectedFrame.offsetTop,
  left: 0,
  behavior: 'smooth'
});

Note : smooth is not supported in Safari.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo


try this one.

window.scrollTo(0, 0);

I have used this to scroll from bottom to top. And it works for me using javascript.

Another way :

HTML

<button onclick="scrollToTop(3000);"></button>

Javascript

 function scrollToTop(scrollDuration) {
  var scrollStep = -window.scrollY / (scrollDuration / 15),
    scrollInterval = setInterval(function(){
    if ( window.scrollY != 0 ) {
        window.scrollBy( 0, scrollStep );
    }
    else clearInterval(scrollInterval); 
 },15);
}

Tags:

Javascript