【发布时间】:2016-05-03 12:57:24
【问题描述】:
$(function(){
function boxtoRight(){
$("#box").animate({marginLeft: 600},3000,'linear',boxtoLeft);
}
function boxtoLeft(){
$("#box").animate({marginLeft: 0},3000,'linear',boxtoRight);
}
boxtoRight();
$(document).keydown(function(e){
if(e.keyCode == 32){
$("#box").css("background","green");
$("#box").css("bottom","200px").css("transition","0.2s");
}
});
$(document).keyup(function(e){
if(e.keyCode == 32){
$("#box").css("background","red");
$("#box").css("bottom","130px").css("transition","0.2s");
}
});
});
#container{
background: #ccc;
width: 600px;
height: 200px;
}
#box{
width: 90px;
height: 90px;
background: red;
position: absolute;
margin-top: 110px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="box"></div>
</div>
问题是当我按下空格键时我试图让框跳起来。现在如果我按空格键,框会跳,但动画不流畅。
另外,如何通过动画,滚动和转到文档右侧将框移动到窗口的中心?
【问题讨论】:
-
我不确定您所说的“使用动画将框移动到窗口屏幕的中心,滚动并转到文档右侧”是什么意思。如果你更清楚地解释你想要什么,我可以更新我的答案。
-
框从左到右开始时始终位于屏幕中心,就像马里奥一样。
标签: jquery scroll jquery-animate css-transitions