当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML animationiteration事件用法及代码示例


当重复CSS动画时,animationiteration事件发生。

如果CSS animation-iteration-count属性设置为“1”,则只能播放one-time动画。动画需要运行多次才能触发此事件。

CSS动画播放时可能发生的事件:


  • animationstart:CSS动画开始时发生。
  • animationiteration:重复CSS动画时发生。
  • animationend:CSS动画完成时发生。

用法:

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

例:

<!DOCTYPE html> 
<html> 
  
<head> 
    <style> 
        #div { 
            width:100%; 
            height:100px; 
            background:green; 
            position:relative; 
            font-size:40px; 
        } 
        
        /* Chrome, Safari, Opera */ 
        @-webkit-keyframes mymove { 
            from { 
                top:0px; 
            } 
            to { 
                top:200px; 
            } 
        } 
          
        @keyframes mymove { 
            from { 
                top:0px; 
            } 
            to { 
                top:200px; 
            } 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
  
        <div id="div" onclick="GFGFun()"> 
            Click me to start the animation. 
        </div> 
  
        <script> 
            var x = document.getElementById("div"); 
  
            // Start the animation with JavaScript 
            function GFGFun() { 
                // Code for Chrome, Safari and Opera 
                x.style.WebkitAnimation = "mymove 4s 2"; 
                // Standard syntax 
                x.style.animation = "mymove 4s 2"; 
            } 
  
            // Code for Chrome, Safari and Opera 
            x.addEventListener("webkitAnimationStart", StartFun); 
            x.addEventListener("webkitAnimationIteration", RepeatFun); 
  
            // Standard syntax 
            x.addEventListener("animationstart", StartFun); 
            x.addEventListener("animationiteration", RepeatFun); 
  
            function StartFun() { 
                this.innerHTML = "The animation has started"; 
                this.style.backgroundColor = "lime"; 
            } 
  
            function RepeatFun() { 
                this.innerHTML = "Animation was played again"; 
                this.style.backgroundColor = "darkgreen"; 
            } 
        </script> 
    </center> 
</body> 
  
</html>

输出:

支持的浏览器:下面列出了DOM animationiteration Event支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • Firefox
  • 苹果Safari
  • Opera


相关用法


注:本文由纯净天空筛选整理自Vijay Sirra大神的英文原创作品 HTML | DOM animationiteration Event。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。