scroll dyrect to top of the page in javascript

Viewed 21

i have used onclick event

    scrolltoTop= async () => {
        window.scrollTo(0,0)
     }

but it takes more time to go top so how to go top dyrect using javascript, and if it is not support than how to fast scroll?

1 Answers

Instead of window.scrollTo(xCoord, yCoord); you can use scrollIntoView() like so:

<html>
  <body>
   <div id="top"></div>
   .....
  </body>
</html>

<script>
  document.getElementById("top").scrollIntoView();
</script>
Related