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

Gan*_*row 43 javascript jquery

点击某个锚时,我希望我的页面转到顶部.

这是我试图这样做但它不起作用,它滚动速度超快.

 $('a[href=#top]').click(function () {
        $('body').animate({
                scrollTop: 0
        },
        50);
});
Run Code Online (Sandbox Code Playgroud)

我想放慢速度.

ant*_*ant 104

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

也许?

  • window是veiwport但是你需要为文档html设置动画,你也不需要body $("html").animate({scrollTop:$("#whatever_id_you_want_to_go_to").offset().top},1200); (6认同)
  • body由webkit浏览器使用,html由firefox使用. (4认同)
  • 我认为你确实需要`body`,因为`html`不适用于所有浏览器. (2认同)

wsa*_*lle 10

将50作为第二个参数传递给动画时,即50毫秒.请参阅animate文档.要么传递更大的数字,要么建议c0mrade,只需传递'慢'.