下面的函数_anim
保证运行所有动画都是有序的。它从上一个的完成回调执行下一个动画。它还包括一个可选的回调函数,该函数在所有动画完成时执行。
这是实际的代码。http://jsfiddle.net/Hrkee/
$("#Sim2").click(function () {
var change = [9,4];
_anim(change, 0, { left: '50%' }, 500, function() { alert('all complete'); });
});
function _anim(aryKeys, index, properties, duration, callback) {
$("#number" + aryKeys[index]).animate(properties, duration, function(){
if(index + 1 <= aryKeys.length - 1){
_anim(aryKeys, index + 1, properties, duration, callback);
} else {
if(callback)
callback();
}
});
}
仅供参考,我从我写的关于同步加载异步 javascript 的博客文章中窃取了代码。http://www.mobtowers.com/load-cross-domain-javascript-synchronously-using-jquery/