我想平滑地向下滚动。我不想为此写一个函数——特别是如果jQuery已经有了一个。
当前回答
$(".scroll-top").on("click", function(e){
e.preventDefault();
$("html, body").animate({scrollTop:"0"},600);
});
其他回答
你可以使用.animate() scrollTop属性,就像这样:
$("html, body").animate({ scrollTop: "300px" });
试试scrollTo插件。
如果你想在页面的末尾向下移动(所以你不需要向下滚动到底部),你可以使用:
$('body').animate({ scrollTop: $(document).height() });
$(".scroll-top").on("click", function(e){
e.preventDefault();
$("html, body").animate({scrollTop:"0"},600);
});
尼克的回答很好,默认设置也很好,但你可以通过完成所有可选设置来更全面地控制滚动。
下面是它在API中的样子:
.animate( properties [, duration] [, easing] [, complete] )
所以你可以这样做:
.animate(
{scrollTop:'300px'},
300,
swing,
function(){
alert(animation complete! - your custom code here!);
}
)
这是jQuery .animate函数的api页面:http://api.jquery.com/animate/