javascript jquery - 单击时动画返回到页面顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4518862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery - Animated return to top of page on click
提问by Tom
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?
有谁知道如何做到这一点?
回答by Nick Craver
You want to .animate()the scrollTopproperty to 0, like this:
您希望.animate()该scrollTop属性为0,如下所示:
$("html, body").animate({ scrollTop: 0 }, 500);
回答by Jimmy Huang
you can try this:
你可以试试这个:
$('#somewhere').click(function(){
$('html, body').animate({scrollTop:0}, 'slow');
});
回答by user3645907
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);
回答by Mark Steggles
I have used Ariel Flesler's localscroll plugin to do this many times... http://demos.flesler.com/jquery/localScroll
我已经多次使用 Ariel Flesler 的 localscroll 插件来执行此操作... http://demos.flesler.com/jquery/localScroll

