W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!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; /* Chrome, Safari, Opera */ animation: mymove 4s infinite; } /* Chrome, Safari, Opera */ @-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"); // 针对 Chrome、Safari 和 Opera 的代码 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>