W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <head> <style> #myDIV { margin: 25px; width: 550px; height: 100px; background: orange; position: relative; font-size: 20px; } /* 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" 以及 "animationend" 事件附加到 DIV 元素。</p> <div id="myDIV" onclick="myFunction()">点击我开始动画</div> <script> var x = document.getElementById("myDIV"); // Start the animation with JavaScript function myFunction() { x.style.WebkitAnimation = "mymove 4s 2"; // 针对 Chrome、Safari 和 Opera 的代码 x.style.animation = "mymove 4s 2"; // 标准语法 } // 针对 Chrome、Safari 和 Opera 的代码 x.addEventListener("webkitAnimationStart", myStartFunction); x.addEventListener("webkitAnimationIteration", myRepeatFunction); x.addEventListener("webkitAnimationEnd", myEndFunction); // 标准语法 x.addEventListener("animationstart", myStartFunction); x.addEventListener("animationiteration", myRepeatFunction); x.addEventListener("animationend", myEndFunction); function myStartFunction() { this.innerHTML = "animationstart event occured - The animation has started"; this.style.backgroundColor = "pink"; } function myRepeatFunction() { this.innerHTML = "animationiteration event occured - The animation was played again"; this.style.backgroundColor = "lightblue"; } function myEndFunction() { this.innerHTML = "animationend event occured - The animation has completed"; this.style.backgroundColor = "lightgray"; } </script> </body> </html>