Scroll to top of the page using jquery animate

Narendra Bista picture Narendra Bista · May 2, 2013 · Viewed 36.4k times · Source

I want to put to the top bottom of the page which should scroll the page to the top. However I want the scrolling process to have animated effect. I think it is possible by using animate but have no idea how to ?

Please help. Thanks

Answer

Paras picture Paras · May 2, 2013

The way is pretty simple. Put a button at the bottom of the page and write an event something like this

$('#spnTop').on("click",function() {
    $('html, body').animate({ scrollTop: 0 }, 'slow', function () {
        alert("reached top");
    });
});

Here is the fiddle for this