Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - Animated return to top of page on click

I want to animate a scroll effect to take a user to the top of the page when clicking on an element. A bit like anchoring to the top of the page but smoother.

I've seen this done (can't remember where though).

Does anyone know how this can be done?

like image 668
Tom Avatar asked Dec 23 '10 12:12

Tom


2 Answers

you can try this:

 $('#somewhere').click(function(){
   $('html, body').animate({scrollTop:0}, 'slow');
 });
like image 94
Jimmy Huang Avatar answered Nov 11 '22 17:11

Jimmy Huang


You want to .animate() the scrollTop property to 0, like this:

$("html, body").animate({ scrollTop: 0 }, 500);
like image 35
Nick Craver Avatar answered Nov 11 '22 15:11

Nick Craver