Javascript – How to scroll to top when a button is clicked


I have a button on a page and I want that when it is clicked, the page scrolls to the 0 position.


<button id="button">Back to top </button>



        var scroll_pos = $(window).scrollTop()
    if(scroll_pos > 10){


        // what code to enter here??



Best Solution

Try this code:

$("#button").on("click", function() {

Method on binds a click event to the button and scrollTop scrolls your body to 0 position.

Related Question