Gandalf StormCrow
Gandalf StormCrow

Reputation: 26202

Slow down scroll to top event by jQuery animate

I'd like my page to go to the top when certain anchor is clicked.

Here is how I tried to do it but it's not working, it's scrolling super fast.

 $('a[href=#top]').click(function () {
        $('body').animate({
                scrollTop: 0
        },
        50);
});

I want to slow it down.

Upvotes: 48

Views: 104974

Answers (4)

Yassine Khachlek
Yassine Khachlek

Reputation: 1134

$('a[href=\\#top]').click(function(){
  $('body').animate(
    {
      scrollTop: 0
    }, 
    2000
  );
});

The # should be escaped \\#.

Upvotes: 0

Gireesh T
Gireesh T

Reputation: 57

you can set the time for scroll top

$('a[href=#top]').click(function(){
 $('body').animate({
     scrollTop: 0},4000);});

Upvotes: 0

ant
ant

Reputation: 22948

$('a[href=#top]').click(function(){
    $('html, body').animate({scrollTop:0}, 'slow');
});

Perhaps?

Upvotes: 115

wsanville
wsanville

Reputation: 37516

When you pass 50 as the second parameter to animate, that is 50 milliseconds. See the animate documentation. Either pass a larger number, or as c0mrade suggested, simply pass 'slow' .

Upvotes: 10

Related Questions