2017-07-15 92 views
0

假設我有從頂部到底部帶來紅色方塊的動畫 - 每5秒發生一次。一旦方格在5秒後下降到最低點,腳本會將我們的紅色方塊傳回到頂部,這樣循環可以從頂部重新開始。事情看起來並不自然。CSS動畫循環 - 下一個循環應該從前一個循環開始

我想要實現的是從底部到頂部順利地開始第二個循環,然後再從頂部到底部等,使運動看起來自然而平滑。換句話說 - 紅色方塊會每5秒從上到下反彈。有什麼建議麼?

+0

單動畫循環應該有充分的循環(頂>下來,5S,向下>頂部,5S)。然後你可以循環運行它。 – mx0

+0

你可以請更具體嗎?就像你剛纔所說的虛擬例子? –

+1

此網站以其他方式工作。使用迄今爲止嘗試過的代碼([MCVE](https://stackoverflow.com/help/mcve))更新您的問題,有人可能會幫助您, – mx0

回答

0

我認爲你正在尋找。

可能會幫助你。

div { 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: red; 
 
    position: relative; 
 
    -webkit-animation-name: example; 
 
    -webkit-animation-duration: 4s; 
 
    -webkit-animation-iteration-count: 3; 
 
    -webkit-animation-direction: alternate; 
 
    animation-name: example; 
 
    animation-duration: 4s; 
 
    animation-iteration-count: 3; 
 
    animation-direction: alternate; 
 
} 
 

 
@-webkit-keyframes example { 
 
    0% { 
 
    background-color: red; 
 
    left: 0px; 
 
    top: 0px; 
 
    } 
 
    50% { 
 
    background-color: green; 
 
    bottom: 0px; 
 
    top: 200px; 
 
    } 
 
    100% { 
 
    background-color: red; 
 
    bottom: 0px; 
 
    top: 0px; 
 
    } 
 
} 
 

 
@keyframes example { 
 
    0% { 
 
    background-color: red; 
 
    left: 0px; 
 
    top: 0px; 
 
    } 
 
    50% { 
 
    background-color: green; 
 
    bottom: 0px; 
 
    top: 200px; 
 
    } 
 
    100% { 
 
    background-color: red; 
 
    bottom: 0px; 
 
    top: 0px; 
 
    } 
 
}
<div></div>

+0

這可能是'animation-direction:alternate'。我一直在尋找。謝謝你的朋友! –