JavaScript – Scroll to Top

Leave a comment

In a scenario where we need to scroll to the top of a web page, we can use JavaScript’s scrollTo. Consider the scenario where there is a page filled with text, and the scroll bar appears.

With a scroll button at the bottom:

We can add the following code to set the window to it’s x/y coordinates:

    window.scrollTo(0,0);

Full code:

When clicking the button, the page will scroll to the top:

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *