开发者

jquery - Animated return to top of page on click

开发者 https://www.devze.com 2023-01-31 18:49 出处:网络
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开发者_如何学JAVA to the top of the page but smoother.

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开发者_如何学JAVA 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?


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

$("html, body").animate({ scrollTop: 0 }, 500);


you can try this:

 $('#somewhere').click(function(){
   $('html, body').animate({scrollTop:0}, 'slow');
 });


You can put this code on document load, for example to animate to top

$("html, body").animate({ scrollTop: 0 }, 1000);

And you can animate to an especific element, like

$("html, body").animate({ scrollTop: $('.my-element').offset().top }, 1000);


I have used Ariel Flesler's localscroll plugin to do this many times... http://demos.flesler.com/jquery/localScroll

0

精彩评论

暂无评论...
验证码 换一张
取 消