在CSS3中,我們可以使用animation-duration屬性來設置動畫的持續時間,也就是完成從0%到100%所使用的總時間。animation-duration屬性跟CSS3過渡中的transition-duration屬性相似。
語法:
animation-duration:時間;
說明:
animation-duration屬性取值是一個時間,單位為s(秒),可以為小數如0.5s。
舉例:
在線測試
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CSS3 animation-duration屬性</title>
<style type="text/css">
@-webkit-keyframes mytranslate
{
0%{}
100%{-webkit-transform:translateX(100px);}
}
div:not(#container)
{
width:40px;
height:40px;
border-radius:20px;
background-color:red;
-webkit-animation-name:mytranslate;
-webkit-animation-timing-function:linear;
}
#container
{
display:inline-block;
width:140px;
border:1px solid silver;
}
#div1{-webkit-animation-duration:2s;margin-bottom:10px;}
#div2{-webkit-animation-duration:4s;}
</style>
</head>
<body>
<div id="container">
<div id="div1"></div>
<div id="div2"></div>
</div>
</body>
</html>
在浏覽器預覽效果如下:
分析:
這個例子中,我們設置#div1的元素動畫持續時間為2s,而設置#div2的元素動畫持續時間為4s。從在線測試中,我們可以明顯看出效果。
這裡說一句,CSS3動畫很多時候都是跟CSS3變形結合起來,然後實現絢麗復雜的動畫效果。