缩短 jQuery 动画功能并恢复为按键时启动
Cut jQuery Animate Function Short and Revert to Start on Keypress
我有一个简单的落球动画,我正在使用 jQuery 动画功能。球从 bottom: 600
开始下落,直到到达地面 bottom: 90
。
function ballbounce() {
$('.football').animate({
'bottom': 90
}, bouncetime, 'easeOutBounce', function() {
});
}
当按下空格键时,我想缩短下落动画,让球再次上升。我正在尝试通过检测按键,获取球的当前 bottom
值,将 300px 添加到该值,然后将其设置为球上的新 bottom
值。但是我希望下落动画从那个点恢复。
$(document).keydown(function(e) {
switch(e.which) {
case 32: // spacebar
var ballOffset = $('.football').offset();
var currentBallPosition = $(window).height() - ballOffset.top - $('.football').height();
var newBallPosition = currentBallPosition + 300;
$('.football').css("bottom", newBallPosition);
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action
});
我无法让它工作,我不确定哪里出错了。球的 bottom
只有在动画完成后按空格键才会更新。
这是一款基本的快活足球游戏。所以球掉了下来,你按下空格键再次将球踢回空中。这甚至是一个好的解决方案吗?
我会在元素上调用 .stop()
,然后将其重置为原始位置。您可以在动画之前将原始位置存储为元素本身的数据属性,然后使用它来重置它,如下所示:
function ballbounce(selector,bouncetime) {
var $ball= $(selector);
$ball.data('original-top',$ball.position().top )
$ball.animate({ top: '+550px'}, bouncetime, "easeOutBounce", function() {
//animation complete
});
return $ball;
}
var $ballReference=ballbounce('.football',5000);
$(document).keydown(function(e) {
switch(e.which) {
case 32: // spacebar
$ballReference.stop().css({top:$ballReference.data('original-top')});
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action
});
.football{
width:100px;
height:100px;
background-color:#ccc;
position:absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<div class="football"></div>
我有一个简单的落球动画,我正在使用 jQuery 动画功能。球从 bottom: 600
开始下落,直到到达地面 bottom: 90
。
function ballbounce() {
$('.football').animate({
'bottom': 90
}, bouncetime, 'easeOutBounce', function() {
});
}
当按下空格键时,我想缩短下落动画,让球再次上升。我正在尝试通过检测按键,获取球的当前 bottom
值,将 300px 添加到该值,然后将其设置为球上的新 bottom
值。但是我希望下落动画从那个点恢复。
$(document).keydown(function(e) {
switch(e.which) {
case 32: // spacebar
var ballOffset = $('.football').offset();
var currentBallPosition = $(window).height() - ballOffset.top - $('.football').height();
var newBallPosition = currentBallPosition + 300;
$('.football').css("bottom", newBallPosition);
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action
});
我无法让它工作,我不确定哪里出错了。球的 bottom
只有在动画完成后按空格键才会更新。
这是一款基本的快活足球游戏。所以球掉了下来,你按下空格键再次将球踢回空中。这甚至是一个好的解决方案吗?
我会在元素上调用 .stop()
,然后将其重置为原始位置。您可以在动画之前将原始位置存储为元素本身的数据属性,然后使用它来重置它,如下所示:
function ballbounce(selector,bouncetime) {
var $ball= $(selector);
$ball.data('original-top',$ball.position().top )
$ball.animate({ top: '+550px'}, bouncetime, "easeOutBounce", function() {
//animation complete
});
return $ball;
}
var $ballReference=ballbounce('.football',5000);
$(document).keydown(function(e) {
switch(e.which) {
case 32: // spacebar
$ballReference.stop().css({top:$ballReference.data('original-top')});
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action
});
.football{
width:100px;
height:100px;
background-color:#ccc;
position:absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<div class="football"></div>