2010-06-21 109 views
36

我在一分鐘內製作一個CSS動畫,並在其中移動東西,並希望它停留在最後位置,直到用戶將鼠標移開。無法在一個循環結束時停止動畫

body { 
    background: url('osx.jpg'); 
    padding: 0; 
    margin: 0; 
    line-height: 60px; 
} 

@-webkit-keyframes item1 { 
    0% { bottom: -120px; left: 0px; } 
    10% { bottom: -40px; left: 10px; -webkit-transform: rotate(5deg); } 
    100% { bottom: -40px; left: 10px; -webkit-transform: rotate(5deg); } 
    } 

@-webkit-keyframes item2 { 
    0% { bottom: -120px; left: 0px; } 
    10% { bottom: 60px; left: 20px; -webkit-transform: rotate(7deg); } 
    100% { bottom: 60px; left: 20px; -webkit-transform: rotate(7deg); } 
} 

@-webkit-keyframes item3 { 
    0% { bottom: -120px; left: 0px; } 
    10% { bottom: 160px; left: 30px; -webkit-transform: rotate(9deg); } 
    100% { bottom: 160px; left: 30px; -webkit-transform: rotate(9deg); } 
} 

    div { 
    position: relative; 
    } 
#folder { 
    width: 120px; 
    margin: 0px auto; 
} 

#folder > div { 
    position: absolute; 
} 

#folder:hover > div:nth-of-type(1) { 
    -webkit-animation-name: item1; 
    -webkit-animation-duration: 10s; 
} 

#folder:hover > div:nth-of-type(2) { 
    -webkit-animation-name: item2; 
    -webkit-animation-duration: 10s; 
} 

#folder:hover > div:nth-of-type(3) { 
    -webkit-animation-name: item3; 
    -webkit-animation-duration: 10s; 
} 

只要動畫結束,它只是重複自己。我只希望它發生一次,並保持它的位置,直到用戶離開。我嘗試了使用規範中暫停的東西,但它並不像我預期的那樣工作。任何幫助表示讚賞。謝謝。 :)

回答

85

以下評論適合我。感謝邁克爾

「你需要添加填充模式在動畫的最後凍結動畫狀態。

-webkit-animation-fill-mode: forwards; 

forwards離開動畫的最後一幀的狀態

backwards在開始處留下動畫

+1

這工作!謝謝! – 2011-06-13 08:23:23

+0

我知道這有點遲,但正確的方式是動畫播放狀態:https://developer.mozilla.org/en-US/docs/Web/CSS/animation-play-state – Marco 2017-10-18 12:44:55

0

試試這個:

-webkit-animation-duration: 10s, 10s; 

:)

+0

將動畫超過10秒鐘並且超過10秒。 – 2010-06-22 09:59:28

+0

爲你工作? – 2010-06-23 10:19:11

+0

還是什麼都沒有,我決定讓這個動畫真的很長(比如10,000秒),這樣用戶不會在這段時間內懸停在它上面; D – Johnny 2010-07-03 23:25:04

1

的CSS3動畫重置爲默認樣式在動畫的結尾。您可以使用javascript嘗試:

您可以爲動畫添加一個eventListener,檢索想要保留的樣式,手動應用它們並刪除動畫。

document.getElementById('yourdiv').addEventListener(
    'webkitAnimationEnd', 
    function(){ 
     document.getElementById('yourdiv').style.backgroundPosition = '0px 0px'; 
     document.getElementById('yourdiv').style.webkitAnimationName = 'none'; 
    }, 
    false 
); 
+0

這是真的,但是我正在進行的實驗是嘗試並只用CSS來完成。感謝您嘗試 – Johnny 2011-02-17 21:12:09

+5

您需要添加填充模式以凍結動畫結束時的動畫狀態。 「-webkit-animation-fill-mode:forwards」你不應該讓動畫超長 - 這會咀嚼用戶的CPU - CSS動畫非常耗費CPU資源。 – 2011-04-14 18:36:44

+0

@Michael有趣!感謝你,我不知道它存在。我會嘗試它,並將在我的下一個項目中使用它。 – Stef 2011-04-15 01:32:14

2

如果我正確地理解了這個問題,聽起來好像你需要設置迭代爲'1'。

-webkit-animation-iteration-count: 1; 
+0

就是這樣,謝謝一堆! – 2017-02-15 12:03:37

8

爲了防止動畫重置爲第一幀,請注意訂單,其中聲明瞭css:

這是工作的罰款:

animation: yourAnimationName 1s; 
    animation-fill-mode: forwards; 

這不會(復位到第一幀):

animation-fill-mode: forwards; 
    animation: yourAnimationName 1s; 

整蠱!

+0

謝謝!任何理由爲什麼這是? – mariachimike 2014-01-09 00:35:41

+0

發生這種情況是因爲'animation'是所有'animation- *'屬性的縮寫,並且設置'animation'會覆蓋所有'animation- *'屬性。這種行爲與其他的短手勢(例如'background'或'border')相同。如果您使用'動畫'縮寫並且不定義填充模式,則默認情況下將使用'none'。 – 2015-05-08 06:10:57

+0

好點!我不知道爲什麼這發生在我身上! – 2015-09-09 10:15:42