使用 jquery animate 滚动到页面顶部

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

Scroll to top of the page using jquery animate

jqueryscrolltop

提问by Narendra Bista

I want to put to the top bottom of the page which should scroll the page to the top. However I want the scrolling process to have animated effect. I think it is possible by using animate but have no idea how to ?

我想放在页面的顶部底部,应该将页面滚动到顶部。但是我希望滚动过程具有动画效果。我认为使用 animate 是可能的,但不知道如何使用?

Please help. Thanks

请帮忙。谢谢

回答by Paras

The way is pretty simple. Put a button at the bottom of the page and write an event something like this

方法很简单。在页面底部放置一个按钮并编写一个类似这样的事件

$('#spnTop').on("click",function() {
    $('html, body').animate({ scrollTop: 0 }, 'slow', function () {
        alert("reached top");
    });
});

Here is the fiddlefor this

这里是小提琴

回答by pai.not.pi

You can animate the scrollTopproperty of the html, body

您可以动画的scrollTop属性html, body

$('html, body').animate({
    scrollTop: "0px"
}, 800);

I suggest you do some research/reading before you ask trivial questions like these. Google can help you out before all of us can.

我建议你在问这些琐碎的问题之前先做一些研究/阅读。Google 可以先于我们所有人为您提供帮助。