简体   繁体   中英

scroll dyrect to top of the page in javascript

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM