<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
margin: 25px;
width: 550px;
height: 100px;
background: orange;
position: relative;
font-size: 20px;
text-align: center;
-webkit-animation: mymove 4s infinite;
animation: mymove 4s infinite;
}
@-webkit-keyframes mymove {
from {top: 0px;}
to {top: 200px;}
}
@keyframes mymove {
from {top: 0px;}
to {top: 200px;}
}
</style>
</head>
<body>
<p>本例使用 addEventListener() 方法将 "animationstart" 和 "animationiteration" 事件附加到 DIV 元素。</p>
<p>当动画重复时,DIV 的背景颜色会是淡蓝色。</p>
<p>在本例中,elapsedTime 属性返回<b>重复动画</b>已运行的秒数:</p>
<p><b>注释:</b>IE9 及更早版本不支持 elapsedTime 属性。</p>
<div id="myDIV">动画开始了</div>
<script>
var x = document.getElementById("myDIV");
x.addEventListener("webkitAnimationIteration", myRepeatFunction);
x.addEventListener("animationiteration", myRepeatFunction);
function myRepeatFunction(event) {
this.style.backgroundColor = "lightblue";
this.innerHTML = "Elapsed time: " + event.elapsedTime + " seconds";
}
</script>
</body>
</html>