回答
两种其他解决方案都是正确的,但请,请不要让您的代码列表如果其他条件与一个巨大的内部块。考虑,而不是下面的解决方案:
$(document).ready(function(){
animateTheBox('block1',0);
animateTheBox('block2',2);
});
function animateTheBox(block,i) {
var animation = {};
var duration = 3000;
var easing = 'linear';
var done = function(){ animateTheBox(block, (i+1)%4); };
switch(i){
case 0: animation = {'left' : "-=100px"}
break;
case 1: animation = {'top' : "-=100px"}
break;
case 2: animation = {'left' : "+=100px"}
break;
case 3: animation = {'top' : "+=100px"}
break;
default: return; //This is so you can't call the function in weird ways, as before.
}
$('.' + block).animate(animation, duration, easing, done);
}
使用switch语句来决定做什么样的动画,然后只写实际的动画调用一次。这种抽象更容易阅读,并且具有更易维护的附加好处。你可以确定你的动画每次都会以同样的方式完成。
编辑:
虽然上面的设计模式可能是从长远来看更好,你可以很容易地使用数组,而不是这样做:
$(document).ready(function(){
animateTheBox('block1',0);
animateTheBox('block2',2);
});
function animateTheBox(block,i) {
var animations = [
{'left' : "-=100px"}
, {'top' : "-=100px"}
, {'left' : "+=100px"}
, {'top' : "+=100px"}
];
var duration = 3000;
var easing = 'linear';
var done = function(){ animateTheBox(block, (i+1)%4); };
if (i < 0 || i >= animations.length)
return; //Don't deal with out of bound numbers.
$('.' + block).animate(animations[i], duration, easing, done);
}
而实际上,这可以使多步骤动画抽象变得非常简单:
$(document).ready(function(){
var block1Steps = [
{'left' : "-=100px"}
, {'top' : "-=100px"}
, {'left' : "+=100px"}
, {'top' : "+=100px"}
];
var block2Steps = [
{'left' : "+=100px"}
, {'top' : "+=100px"}
, {'left' : "-=100px"}
, {'top' : "-=100px"}
];
multiAnimate($('.block1'), block1Steps, 3000, 'linear', true);
multiAnimate($('.block2'), block2Steps, 3000, 'linear', true);
});
function multiAnimate(item, animations, duration, easing, infinite){
var i = -1;
var step = function(){
i++;
if (infinite)
i %= animations.length;
if (i >= animations.length) return;
item.animate(animations[i], duration, easing, step);
};
step();
}
然后,如果你想获得真正Apeshit,你可以给每个动画自己的时间和放松,和BAM!你基本上已经为自己创建了一个任意的多步骤动画库。
function multiAnimate(item, animations, duration, easing, infinite){
var defaultDuration = 1000;
var defaultEasing = 'linear';
var i = -1;
var step = function(){
i++;
if (infinite)
i %= animations.length;
if (i >= animations.length) return;
item.animate(animations[i].animation
, (animations[i].duration)? animations[i].duration: defaultDuration
, (animations[i].easing)? animations[i].easing: defaultEasing
, step
);
};
step();
}
$(document).ready(function(){
var block1Steps = [
{
animation: {'left' : "-=100px"}
, duration: 3000
, easing: 'linear'
}
, {
animation: {'top' : "-=100px"}
, duration: 1000
, easing: 'swing'
}
, {
animation: {'left' : "+=100px"}
, duration: 5000
, easing: 'swing'
}
, {
animation: {'top' : "+=100px"}
, duration: 2000
, easing: 'linear'
}
];
var block2Steps = [
{
animation: {'left' : "+=100px"}
, duration: 5000
, easing: 'swing'
}
, {
animation: {'top' : "+=100px"}
, duration: 2000
, easing: 'linear'
}
, {
animation: {'left' : "-=100px"}
, duration: 3000
, easing: 'linear'
}
, {
animation: {'top' : "-=100px"}
, duration: 1000
, easing: 'swing'
}
];
multiAnimate($('.block1'), block1Steps, 3000, 'linear', true);
multiAnimate($('.block2'), block2Steps, 3000, 'linear', true);
});
+1我已经替换了其他编码器的次数,如果还有其他编码器,如果还有其他编码器的话...... – Popnoodles 2013-03-15 00:12:13
@popnoodles - 你是对的。我添加了几个迭代。并不是所有这些都会减少行数,但我喜欢将它们中的每一个都视为一种改进。 – FrankieTheKneeMan 2013-03-15 00:37:47
谢谢你的提示:D – smotru 2013-03-15 23:06:11
的原因是,你需要等待第一个动画告诉箱子,开始下一组动画之前完成。在你的代码,你不给的红色方块的机会,开始动画,因为黄色的不断做(有由animateTheBox
和两个框创建了一个封闭的调用它):)
所以我添加完成功能处理程序到您的.animate()
并将animateTheBox()
呼叫移到那里。
谢谢!试图了解现在:) – smotru 2013-03-15 00:01:40
您需要使用每个动画完成函数来启动下一个动画这样的:
$(document).ready(function(){
animateTheBox('block1',0);
animateTheBox('block2',2);
});
function animateTheBox(block,i) {
if (i==0)
{
$('.'+block).animate({'left' : "-=100px"}, 3000, 'linear', function() {
animateTheBox(block,1);
});
}
else if (i==1)
{
$('.'+block).animate({'top' : "-=100px"}, 3000, 'linear', function() {
animateTheBox(block,2);
});
}
else if (i==2)
{
$('.'+block).animate({'left' : "+=100px"}, 3000, 'linear', function() {
animateTheBox(block,3);
});
}
else if (i==3)
{
$('.'+block).animate({'top' : "+=100px"}, 3000, 'linear', function() {
animateTheBox(block,0);
});
}
}
工作演示:http://jsfiddle.net/jfriend00/39SUN/
干的精神,这里有一个更简短的方法:
$(document).ready(function(){
animateTheBox('.block1',0);
animateTheBox('.block2',2);
});
function animateTheBox(block,i) {
var anims = [
{left: "-=100px"},
{top: "-=100px"},
{left: "+=100px"},
{top: "+=100px"},
];
$(block).animate(anims[i], 3000, 'linear', function() {
animateTheBox(block,(i+1) % 4);
});
}
- 1. 同时运行两个动画iphone
- 2. 如何同时运行两个jQuery的动画?
- 3. 在CSS循环上运行两个不同的同时动画?
- 4. 如何运行单个元素上的两个jQuery的动画同时
- 5. 同时运行两个单独的jQuery
- 6. 同时运行分组动画jQuery
- 7. 同时运行两个jQuery函数
- 8. 在android中同时运行两个动画
- 9. 我们如何才能同时运行两个动画?
- 10. jquery动画运行两次后单击
- 11. 动画不同时间的两个div
- 12. 同步从jQuery运行CSS动画
- 13. 同时在一个动画中同时放大两个对象动作jQuery
- 14. 在相同的运行时间运行两个相同的DLL?
- 15. 运行两个实例的VLC同时
- 16. 运行两个CGI的同时
- 17. 两个动画同时淡入?
- 18. 如何使用CSS3与jQuery同时运行关键帧动画
- 19. jquery动画运行laggy
- 20. 联合运行jQuery动画
- 21. 加速运行jquery动画
- 22. 同时运行jQuery的多个版本
- 23. 如何让两个jQuery动画同时播放?
- 24. 动画两个div在同一时间onload,jquery帮助
- 25. 同时运行两个定时器
- 26. jQuery为两个不同的事件运行相同的代码
- 27. jQuery的动画:分割一个动画两个部分
- 28. JS - 画布 - 同时画两个画布 - ?喂两个变量?
- 29. 与不同的睡眠时间同时运行两个线程
- 30. 如何暂停正在运行的动画并使用jquery中的一个按钮运行另一个动画
你真的应该把你的代码在你的问题等哪天在当的jsfiddle走了以后,这个问题仍然可以被别人用来学习。 – jfriend00 2013-03-15 00:21:48