Javascript 通过 jQuery 动画减慢滚动到顶部事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2123690/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:50:11  来源:igfitidea点击:

Slow down scroll to top event by jQuery animate

javascriptjquery

提问by Gandalf StormCrow

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.

我想慢下来。

回答by ant

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

Perhaps?

也许?

回答by wsanville

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

当您将 50 作为第二个参数传递给动画时,即为 50 毫秒。请参阅动画文档。要么传递更大的数字,要么按照 c0mrade 的建议,简单地传递 'slow' 。

回答by Gireesh T

you can set the time for scroll top

您可以设置滚动顶部的时间

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

回答by Yassine Khachlek

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

The # should be escaped \\#.

# 应该被转义为 \\#。