Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to top of the page using jquery animate

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

like image 298
Narendra Bista Avatar asked May 02 '13 09:05

Narendra Bista


People also ask

How do I scroll to the top of the page using jQuery?

In jQuery, the scrollTo() method is used to set or return the vertical scrollbar position for a selected element. This behavior can be used to scroll to the top of the page by applying this method on the window property. Setting the position parameter to 0 scrolls the page to the top.

How do you scroll automatically to the top of the page using JavaScript?

You might need to trigger scrolling within JavaScript, in which case: window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.

How do I get back to top button in jQuery?

This code must be added just after we included jQuery, in new <script></script> tags: $(function () { $(window). scroll(function() { if ($(this). scrollTop() - 200 > 0) { $('#to-top').

What is $( window scrollTop ()?

The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.


1 Answers

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

like image 55
Paras Avatar answered Oct 17 '22 09:10

Paras