简体   繁体   English

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

[英]Slow down scroll to top event by jQuery animate

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. 我想放慢速度。

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

Perhaps? 也许?

When you pass 50 as the second parameter to animate, that is 50 milliseconds. 将50作为第二个参数传递给动画时,即50毫秒。 See the animate documentation. 请参阅animate文档。 Either pass a larger number, or as c0mrade suggested, simply pass 'slow' . 要么传递更大的数字,要么建议c0mrade,只需传递'慢'。

you can set the time for scroll top 您可以设置滚动顶部的时间

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

The # should be escaped \\\\#. #应该被转义\\#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM