2012-07-08 45 views
0

HTML:jQuery:爲什麼我不能重複這個動畫?

<div id="slider"> 
    <img src="images/02.jpg" alt="" class="two"> 
    <img src="images/01.jpg" alt="" class="one">  
    <img src="images/03.jpg" alt="" class="tri"> 
    <img src="images/04.jpg" alt="" class="four"> 
    <img src="images/05.jpg" alt="" class="five"> 
</div> 

CSS:

.one, .two, .tri, .four, .five {position: absolute;} 
.two {bottom: 0;} 
.tri {left: -348px;} 
.four {left: 348px; top: 19.8em;} 
.five {right: -257px;} 

這是動畫:

var one = $('.one'); 
var two = $('.two'); 
var tri = $('.tri'); 
var four = $('.four'); 
var five = $('.five'); 

$(two, tri, four, five).hide(); 

//first image 
$(one).animate({ 
     'top' : '-17.5em' 
    }, 8000, function() { 
     $(two).show(); 
     $(one).delay(6000).fadeOut(1500); 
    }); 

    //second image 
    $(two).delay(13700).animate({ 
     'bottom' : '-30.7em' 
    }, 11000, function() { 
     $(tri, four, five).show(); 
     $(two).delay(6000).fadeOut(1000); 
    }); 

    //third/fourth/fifth image 
    $(tri).delay(31500).animate({ 
     'left' : '0' 
    }, 600, function() { 
     $(tri).delay(6000).fadeOut(1000); 
    }); 

    $(four).delay(31500).animate({ 
     'top' : '0' 
    }, 600, function() { 
     $(four).delay(6000).fadeOut(1000); 
    }); 

    $(five).delay(31500).animate({ 
     'right' : '0' 
    }, 600, function() { 
     $(one).delay(5000).css({'top' : '0'}).fadeIn(); 
     $(five).delay(6000).fadeOut(1000); 
    }); 

(我試過把它變成回調函數,但不能使它工作,所以我使用延遲,我知道這是一個不同的問題,但如果任何人有任何想法爲什麼回調不起作用,請告訴)

無論如何,我試圖讓這個動畫無限期地重複。我試過setInterval,我試過把它放到一個函數中,然後再從內部調用它,通常我可以在網上找到任何可能的解決方案,但似乎沒有任何工作。 任何幫助,你可以給我將不勝感激。

+0

無限重複的東西,一個'setInterval'應該只是罰款。還有其他一些事情正在發生。 – elclanrs 2012-07-08 01:55:07

+0

我知道這是一個麻煩,但你可以請發表一個使用setInterval與上述代碼的例子嗎?我試過了,但它要麼完全不起作用,要麼只運行一次,但不重複。 – shki 2012-07-08 02:01:26

+0

您可以將動畫封裝在回調函數中,並在while循環中調用該函數。也許這有助於:http://forum.jquery.com/topic/looping-jquery-function-animation-solved – 2012-07-08 02:03:42

回答

0

嘗試這件事

var ctr = 0; 
function animate_me(){ 
++ctr; 
//your code; 
console.log('animating... #'+ ctr); 

//setTimeout(animate_me, 6000); 
setTimeout(animate_me, 1500); 
} 

$(function() { 
    animate_me(); 
}); 
+0

非常感謝,fedmich。我試過了,它也做了同樣的事情:它運行一次,然後什麼也不做。我不明白。 – shki 2012-07-08 03:52:29

+0

嘗試一下,看看控制檯。它會在1.5秒後繼續加1,2,3 – fedmich 2012-07-08 03:54:43