我有一个简单的页面,使用2个函数来回动画框:animateLeft和animateRight。当我打电话动画这种方式,它工作正常:JQuery动画回调
$("#divTestBox3").animate({ "left" : "200px" }, 1000, animateLeft);
然而,当我把这种方式:
$("#divTestBox3").animate({ "left" : "200px" }, 1000, animateLeft());
功能animateLeft()不会等到动画完成之前出现它执行。 animateLeft()立即被调用,导致堆栈溢出。有人能帮我理解这两条线的区别,以及它们为什么会有不同的表现。
完整的应用程序如下所示。
<head>
<script src="../JSCommon/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var stop = false;
$(document).keydown(function(event) {
stop = !stop;
if (!stop)
animateRight();
});
function animateRight() {
if(!stop) {
console.log('animateRight');
$("#divTestBox3").animate({ "left" : "200px" }, 1000, animateLeft);
}
}
function animateLeft() {
if(!stop) {
console.log('animateLeft');
$("#divTestBox3").animate({ "left" : "20px" },1000,animateRight);
}
}
});
</script>
</head>
<body>
<div style="height: 40px;">
<div id="divTestBox3" style="height: 20px; width: 20px; left: 20px; background-color: #89BC38; position: absolute;"></div>
</div>
<p>Press any key to start/stop the animation</p>
</body>