这段代码正在运行它会让我从头到尾回来,但它没有动画。有谁可以帮我这个?
$('.topScroll').click(function(){
$("html, body").animate({scrollTop : "0px"},"fast");
});
您可以尝试使用微秒减慢动画,如下所示:
$("html, body").animate({scrollTop : "0px"},1200);
好吧,因为看起来你还有问题。您发布的代码 - 有效。好吧,既然你没有共享任何HTML,我只能假设你的确切标记和环境。
Event.preventDefault()
工作范例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>topScroll - example</title>
<style>
body { height: 300vh; }
.topScroll { position: absolute; top: 200vh;}
</style>
</head>
<body>
<header id="top">
<h1>Scroll down...</h1>
</header>
<a href="#top" class="topScroll">GO TO TOP</a>
<script src="//code.jquery.com/jquery-3.1.0.js"></script>
<script>
$(document).on('click', '.topScroll', function(ev) {
ev.preventDefault(); // Prevent browser doing default stuff on anchor/button click
$("html, body").animate({scrollTop: "0px"}, 500);
});
</script>
</body>
</html>