這個屬性決定每一輪迴圈要順著播還是倒著播。
語法:
animation-direction:取值;
| 屬性值 | 說明 |
|---|---|
| normal | 每次循環都正向播放(預設值) |
| reverse | 每次循環都反向播放 |
| alternate | 奇數次正向播放,偶數次反向播放 |
舉例:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS3 animation-direction屬性</title>
<style type="text/css">
@-webkit-keyframes mytranslate {
0%{}
100%{-webkit-transform:translateX(100px);}
}
#div1 {
width:40px;
height:40px;
border-radius:20px;
background-color:red;
-webkit-animation-name:mytranslate;
-webkit-animation-timing-function:linear;
-webkit-animation-duration:2s;
}
#container {
display:inline-block;
width:140px;
border:1px solid silver;
}
</style>
</head>
<body>
<div id="container">
<div id="div1"></div>
</div>
</body>
</html>
分析:
預設的 normal 是沿正方向播放(0% 到 100%),小球由左往右滾動。
-webkit-animation-direction:reverse;
加上這行之後,動畫改為反向播放(100% 到 0%),小球就變成由右往左滾。
若把取值改成 alternate,效果會更有意思:第一輪由左往右,第二輪由右往左,於是形成自然的來回擺動。實務上做載入動畫、呼吸效果時,alternate 搭配 infinite 是最常用的組合。