CSS animation-iteration-count 属性 详解
css3基础 2022-07-14 15:51:47小码哥的IT人生shichen
CSS animation-iteration-count 属性
定义和用法
animation-iteration-count
属性定义动画的播放次数。
另请参阅:
CSS3 教程:CSS 动画
HTML DOM 参考手册:animationIterationCount 属性
实例
播放动画三次:
div
{
animation-iteration-count:3;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
div
{
width:100px;
height:100px;
background:red;
position:relative;
animation:mymove 3s;
animation-iteration-count:3;
/* Safari and Chrome */
-webkit-animation:mymove 3s;
-webkit-animation-iteration-count:3;
}
@keyframes mymove
{
from {top:0px;}
to {top:200px;}
}
@-webkit-keyframes mymove /* Safari and Chrome */
{
from {top:0px;}
to {top:200px;}
}
</style>
</head>
<body>
<p><strong>注释:</strong>Internet Explorer 9 以及更早的版本不支持 animation-iteration-count 属性。</p>
<div></div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
技术细节
默认值: | 1 |
---|---|
继承性: | no |
版本: | CSS3 |
JavaScript 语法: | object.style.animationIterationCount=3 |
浏览器支持
表格中的数字注明了完全支持该属性的首个浏览器版本。
带 -webkit-、-moz- 或 -o- 的数字表示使用前缀的首个版本。
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
43.0 4.0 -webkit- |
10.0 | 16.0 5.0 -moz- |
9.0 4.0 -webkit- |
30.0 15.0 -webkit- 12.0 -o- |
完整实例1:
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
<style>
ul, li{margin:0;padding:0;}
li{list-style:none;}
div#wrapper{width: 960px;margin: 0 auto;padding: 0;text-align: left;border:1px solid #c5c5c5;}
#SelArea{float: left;width: 480px;margin:0;padding:0;border: 0px solid #c5c5c5;}
#SelArea p{margin:20px; }
#result{float: left;width: 478px;margin:0;padding:0;border: 0px solid #c5c5c5;}
#CodeArea{width: 440px;margin:0;margin-left: 10px;margin-bottom:10px;padding: 5px;font-family: courier new;color: #222222;background-color: #f1f1f1;border: 1px solid #c3c3c3;}
#DemoArea{width: 450px;height: 280px;margin:0;padding:0;margin-left: 10px;background-color: #ffffff;border: 1px solid #c3c3c3;}
div#MyDIV
{
width:100px;
height:100px;
background:red;
position:relative;
animation:mymove 5s 1;
-moz-animation:mymove 5s 1;
-webkit-animation:mymove 5s 1;
-o-animation:mymove 5s 1;
}
@keyframes mymove
{
from {left:0px;}
to {left:330px;}
}
@-moz-keyframes mymove /* Firefox */
{
from {left:0px;}
to {left:330px;}
}
@-webkit-keyframes mymove /* Safari and Chrome */
{
from {left:0px;}
to {left:330px;}
}
@-o-keyframes mymove /* Opera */
{
from {left:0px;}
to {left:330px;}
}
div#MyDIV
{
animation-iteration-count:1;
-webkit-animation-iteration-count:1;
}
</style>
<body>
<div id="wrapper">
<div id="SelArea">
<h2>CSS 属性:</h2>
<h3>animation-iteration-count:</h3>
<form action="javascript:return false;">
<ul>
<input type="hidden" id="PreSelectedValue" value="" />
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_1" value="1" checked="checked" />1</li>
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_2" value="2" />2</li>
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_3" value="5" />5</li>
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_4" value="infinite" />infinite</li>
</ul>
</form>
</div>
<div id="result">
<h2>结果:</h2>
<div id="DemoArea">
<div id="MyDIV"></div>
</div>
<h2>CSS 代码:</h2>
<pre id="CodeArea">
div#MyDIV
{
animation-iteration-count:<span id="CodeValue">1</span>;
}
</pre>
</div>
<script>
function test_demo_val(strValue)
{
var strId="MyDIV"
document.getElementById(strId).style.animationIterationCount=strValue;
document.getElementById(strId).style.WebkitAnimationIterationCount=strValue;
document.getElementById(strId).style.MozAnimationIterationCount=strValue;
document.getElementById("CodeValue").innerHTML=strValue;
}
function tiy_onload(){
var PreVal=""
PreVal=document.getElementById("PreSelectedValue").value
if (PreVal!=""){
test_demo_val(PreVal)
var x=document.getElementsByTagName("input")
var l=x.length
for (i=0;i<l;i++){
if (x[i].value==PreVal){
x[i].checked=true
}
}
}
}
function test_demo(obj){
test_demo_val(obj.value)
}
tiy_onload()
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
完整实例2:
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
<style>
ul, li{margin:0;padding:0;}
li{list-style:none;}
div#wrapper{width: 960px;margin: 0 auto;padding: 0;text-align: left;border:1px solid #c5c5c5;}
#SelArea{float: left;width: 480px;margin:0;padding:0;border: 0px solid #c5c5c5;}
#SelArea p{margin:20px; }
#result{float: left;width: 478px;margin:0;padding:0;border: 0px solid #c5c5c5;}
#CodeArea{width: 440px;margin:0;margin-left: 10px;margin-bottom:10px;padding: 5px;font-family: courier new;color: #222222;background-color: #f1f1f1;border: 1px solid #c3c3c3;}
#DemoArea{width: 450px;height: 280px;margin:0;padding:0;margin-left: 10px;background-color: #ffffff;border: 1px solid #c3c3c3;}
div#MyDIV
{
width:100px;
height:100px;
background:red;
position:relative;
animation:mymove 5s 1;
-moz-animation:mymove 5s 1;
-webkit-animation:mymove 5s 1;
-o-animation:mymove 5s 1;
}
@keyframes mymove
{
from {left:0px;}
to {left:330px;}
}
@-moz-keyframes mymove /* Firefox */
{
from {left:0px;}
to {left:330px;}
}
@-webkit-keyframes mymove /* Safari and Chrome */
{
from {left:0px;}
to {left:330px;}
}
@-o-keyframes mymove /* Opera */
{
from {left:0px;}
to {left:330px;}
}
div#MyDIV
{
animation-iteration-count:1;
-webkit-animation-iteration-count:1;
}
</style>
<body>
<div id="wrapper">
<div id="SelArea">
<h2>CSS 属性:</h2>
<h3>animation-iteration-count:</h3>
<form action="javascript:return false;">
<ul>
<input type="hidden" id="PreSelectedValue" value="infinite" />
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_1" value="1" />1</li>
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_2" value="2" />2</li>
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_3" value="5" />5</li>
<li><input autocomplete="off" type="radio" name="rpos" onClick="test_demo(this)" id="value_4" value="infinite" />infinite</li>
</ul>
</form>
</div>
<div id="result">
<h2>结果:</h2>
<div id="DemoArea">
<div id="MyDIV"></div>
</div>
<h2>CSS 代码:</h2>
<pre id="CodeArea">
div#MyDIV
{
animation-iteration-count:<span id="CodeValue">1</span>;
}
</pre>
</div>
<script>
function test_demo_val(strValue)
{
var strId="MyDIV"
document.getElementById(strId).style.animationIterationCount=strValue;
document.getElementById(strId).style.WebkitAnimationIterationCount=strValue;
document.getElementById(strId).style.MozAnimationIterationCount=strValue;
document.getElementById("CodeValue").innerHTML=strValue;
}
function tiy_onload(){
var PreVal=""
PreVal=document.getElementById("PreSelectedValue").value
if (PreVal!=""){
test_demo_val(PreVal)
var x=document.getElementsByTagName("input")
var l=x.length
for (i=0;i<l;i++){
if (x[i].value==PreVal){
x[i].checked=true
}
}
}
}
function test_demo(obj){
test_demo_val(obj.value)
}
tiy_onload()
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html