回答
兩種其他解決方案都是正確的,但請,請不要讓您的代碼列表如果其他條件與一個巨大的內部塊。考慮,而不是下面的解決方案:
$(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