Force move to top of browser with jQuery

I have a website where I needed the user when they clicked a button to go back to the top of the page.

For some reason, scrollTop() in jquery did not work and window.scroll in native javascript could not get to work either.

So I added this oneliner suggested by Nizam at:

$('html, body').animate({ scrollTop: 0 }, 0);

It forced the browser to go back to the top when a user clicked a specific button on the page. For my page, I wanted the user to go to the top of a form. The page had a fixed button and I placed the oneliner in the same jQuery function.

They even gave a second oneliner for smooth scrolling:

$('html, body').animate({ scrollTop: 0 }, 'slow');

Very cool solution that solved my issue.

Leave a Reply

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