顧名思義,這個屬性控制動畫是正在播放,還是暫停。
語法:
animation-play-state:取值;
| 屬性值 | 說明 |
|---|---|
| running | 播放動畫(預設值) |
| paused | 暫停動畫 |
舉例:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS3 animation-play-state屬性</title>
<style type="text/css">
@-webkit-keyframes mytranslate {
0%{}
50%{-webkit-transform:translateX(200px);}
100%{}
}
#div1 {
width:40px;
height:40px;
border-radius:20px;
background-color:red;
-webkit-animation-name:mytranslate;
-webkit-animation-timing-function:linear;
-webkit-animation-duration:3s;
-webkit-animation-iteration-count:infinite;
}
#container {
display:inline-block;
width:240px;
border:1px solid silver;
}
</style>
<script src="../App_js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btn_pause").click(function () {
$("#div1").css("-webkit-animation-play-state", "paused");
});
$("#btn_run").click(function () {
$("#div1").css("-webkit-animation-play-state", "running");
});
})
</script>
</head>
<body>
<div id="container">
<div id="div1"></div>
</div>
<div id="control_btn">
<input id="btn_pause" type="button" value="暫停"/>
<input id="btn_run" type="button" value="播放"/>
</div>
</body>
</html>
分析:
這裡用 jQuery 綁定兩個按鈕:按「暫停」時把 animation-play-state 設為 paused,按「播放」時改回 running。效果就跟影片播放器的播放與暫停鍵一樣——動畫停在當下的進度,接著播放時從原位繼續,而不是從頭開始。