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


HTML animationstart事件用法及代码示例


当CSS动画开始播放时,将发生animationstart事件。

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

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

用法:


  • Chrome,Safari和Opera的代码:
    object.addEventListener("webkitAnimationStart", myScript);
  • 标准语法:
    object.addEventListener("animationstart", 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 1";  
                
                // Standard syntax 
                x.style.animation = "mymove 4s 1";  
            } 
  
            // Code for Chrome, Safari and Opera 
            x.addEventListener("webkitAnimationStart", StartFun); 
  
            // Standard syntax 
            x.addEventListener("animationstart", StartFun); 
  
            function StartFun() { 
                this.innerHTML = "The animation has started"; 
                this.style.backgroundColor = "lime"; 
            } 
        </script> 
    </center> 
</body> 
  
</html>

输出:

支持的浏览器:animationstart Event支持的浏览器如下:

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


相关用法


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