animationiteration 事件 | HTML DOM 事件对象

返回到:DOM 对象:HTML DOM 事件对象

定义和用法

animationiteration 事件在 CSS 动画重新播放时触发。

如果 CSS animation-iteration-count 属性设置为 “1”, 动画将只播放一次, animationiteration 事件不再触发。

CSS 动画播放时,会发生以下三个事件:

  • animationstart – CSS 动画开始后触发
  • animationiteration – CSS 动画重复播放时触发
  • animationend – CSS 动画完成后触发

浏览器支持

表格中的数字表示支持该事件的第一个浏览器的版本号。

“webkit” 或 “moz” 后面指定的数字为支持该事件的第一个版本号前缀。

事件谷歌IE火狐苹果opera
animationiteration4.0 webkit10.0 16.0
5.0 moz
4.0 webkit15.0 webkit
12.1

注意: Chrome, Safari 和 Opera 浏览器使用 webkitAnimationEnd 前缀。

语法

object.addEventListener("webkitAnimationIteration", myScript);  // Chrome, Safari 和 Opera代码
object.addEventListener("animationiteration", myScript);        // 标准语法

注意: Internet Explorer 8 及更早 IE 版本不支持 addEventListener() 方法。

技术细节

是否支持冒泡:Yes
是否可以取消:No
事件类型:AnimationEvent

实例

在 CSS 动画重新播放时为 <div> 元素添加监听事件:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程(web176.com)</title>
<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() 方法为 DIV 元素添加"animationstart", "animationiteration" 和 "animationend" 事件。</p>
<div id="myDIV" onclick="myFunction()">点我开始动画</div>
<script>
var x = document.getElementById("myDIV")
// 使用 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", myIterationFunction);
x.addEventListener("webkitAnimationEnd", myEndFunction);
x.addEventListener("animationstart", myStartFunction);
x.addEventListener("animationiteration", myIterationFunction);
x.addEventListener("animationend", myEndFunction);
function myStartFunction() {
    this.innerHTML = "animationstart 事件触发 - 动画已经开始";
    this.style.backgroundColor = "pink";
}
function myIterationFunction() {
    this.innerHTML = "animationiteration 事件触发 - 动画重新播放";
    this.style.backgroundColor = "lightblue";
}
function myEndFunction() {
    this.innerHTML = "animationend 事件触发 - 动画已经完成";
    this.style.backgroundColor = "lightgray";
}
</script>

</body>
</html>

作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4168.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年10月21日 下午4:37
下一篇 2021年10月21日 下午4:43

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注