這個屬性用來決定動畫要播放幾次。
語法:
animation-iteration-count:取值;
說明:
取值有兩種:
預設值為 1,也就是動畫播完一次就停。「animation-iteration-count:n」代表播放 n 次;取值為 infinite 時則會無限循環下去。
舉例:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS3 animation-iteration-count屬性</title>
<style type="text/css">
@-webkit-keyframes mytranslate {
0%{}
50%{-webkit-transform:translateX(100px);}
100%{}
}
#div1 {
width:40px;
height:40px;
border-radius:20px;
background-color:red;
-webkit-animation-name:mytranslate;
-webkit-animation-timing-function:linear;
-webkit-animation-duration:2s;
-webkit-animation-iteration-count:infinite;
}
#container {
display:inline-block;
width:140px;
border:1px solid silver;
}
</style>
</head>
<body>
<div id="container">
<div id="div1"></div>
</div>
</body>
</html>
分析:
設定為 infinite 後,動畫會持續循環播放。試著把「-webkit-transform:translateX(100px);」從 50% 移到 100% 裡面,你會發現節奏感完全不同:前者是「去而復返」的擺動,後者則是「每次重新出發」的跳動。關鍵幀的位置安排,往往比參數本身更影響動畫的觀感。