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开发者_如何学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
精彩评论