Jquery 动画返回顶部
我目前正在尝试制作一个按钮,该按钮将使用户返回页面顶部,但是是动画的。单击按钮时使页面滚动回顶部。我在这里进行了搜索,找到了这个。 ...
我尝试使用以下命令创建 JSfiddle
CSS
html {
margin:0;
padding:0;
height:2000px;
}
body {
height:2000px;
}
#blue-box {
position:fixed;
width:100px;
height:70px;
margin-left:0px;
margin-top:50px;
background-color:blue;
}
#blue-box h1{
font-family:Constantia;
font-size:20px;
text-align:center;
margin-top:5px;
color:#FFFFFF;
}
HTML
<div id="blue-box">
<h1>Back To Top</h1>
</div>
JavaScript
$(document).ready(function() {
var away = false;
$('#blue-box').click(function() {
$("html, body").animate({scrollTop: 0}, 500);
});
不幸的是,这似乎不起作用,我想知道我是否错过了一些东西或做了一些明显错误的事情?
I'm currently trying to make a button which will take the user back to the top of the page, but animated. Making the page scroll back up to the top when the button is clicked. I did a search on here and found this..
And I have attempted to make a JSfiddle of this using the following...
CSS
html {
margin:0;
padding:0;
height:2000px;
}
body {
height:2000px;
}
#blue-box {
position:fixed;
width:100px;
height:70px;
margin-left:0px;
margin-top:50px;
background-color:blue;
}
#blue-box h1{
font-family:Constantia;
font-size:20px;
text-align:center;
margin-top:5px;
color:#FFFFFF;
}
HTML
<div id="blue-box">
<h1>Back To Top</h1>
</div>
JavaScript
$(document).ready(function() {
var away = false;
$('#blue-box').click(function() {
$("html, body").animate({scrollTop: 0}, 500);
});
Unfortunately, this doesn't seem to work and I'm wondering whether I have missed something out or done something obviously wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的脚本末尾缺少
});
。添加它解决您的问题:这是一个很好的例子,说明了为什么正确缩进代码很重要。这使得这种错误更难以被忽视。
You're missing a
});
at the end of your script. Adding it solves your problem:That's a good example of why indenting your code properly is important. It makes this kind of error far more difficult to miss.
您忘记关闭 $(document).ready
http://jsfiddle.net/HX3ww/3/
You forgot to close your $(document).ready
http://jsfiddle.net/HX3ww/3/
这也是一个细微的变化,该按钮仅在您开始向下滚动页面时淡入,并在您返回页面顶部时消失 http://jsfiddle.net/b4M66/
jQuery:
CSS:
HTML:
This is also a slight variation where the button only fades in once you start scrolling down the page and disappears once you return to the top of the page http://jsfiddle.net/b4M66/
jQuery:
CSS:
HTML: