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


HTML Style animationName用法及代码示例


HTML DOM中的animationName属性用于设置或返回@keyframes动画的名称。

用法:

  • 它用于设置animationName属性:
    object.style.animationName = "none|keyframename|initial|inherit"
  • 它用于返回animationName属性:
    object.style.animationName

属性值:它包含动画的名称。


  • none:没有动画时。这是默认值。
  • keyframename:绑定到选择器的关键帧名称。
  • initial:用于设置默认值。
  • inherited:它用于从父级继承属性。

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM animationName Property 
    </title> 
    <style> 
        div { 
            width:100px; 
            height:100px; 
            background:green; 
            position:relative; 
            -webkit-animation:move_1 2s infinite; 
            /* For Chrome, Safari, Opera*/ 
            animation:move_1 2s infinite; 
        } 
        /* For Chrome, Safari, Opera */ 
          
        @-webkit-keyframes move_1 { 
            from { 
                left:150px; 
            } 
            to { 
                left:350px; 
            } 
        } 
          
        @keyframes move_1 { 
            from { 
                left:150px; 
            } 
            to { 
                left:350px; 
            } 
        } 
        /* For Chrome, Safari, Opera */ 
          
        @-webkit-keyframes move_2 { 
            from { 
                left:270px; 
                width:0px; 
                height:0px; 
            } 
            to { 
                left:270px; 
                width:100px; 
                height:100px; 
                background:blue; 
            } 
        } 
          
        @keyframes move_2 { 
            from { 
                left:270px; 
                width:0px; 
                height:0px; 
            } 
            to { 
                left:270px; 
                width:100px; 
                height:100px; 
                background:blue; 
            } 
        } 
    </style> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>DOM animationName Property</h2> 
    <button onclick="Geeks()"> 
        Click Here 
    </button> 
    <br> 
    <br> 
    <div id="GFG_DIV"></div> 
  
    <script> 
        function Geeks() { 
            
            // Changing the animation name. 
            document.getElementById( 
              "GFG_DIV").style.WebkitAnimationName = 
              "move_2";  
            
            // for Chrome, Safari, and Opera 
            document.getElementById( 
              "GFG_DIV").style.animationName =  
              "move_2"; 
        } 
    </script> 
</body> 
  
</html>

输出

  • 在单击按钮之前:
  • 单击按钮后:
  • 最后:

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM animationName Property 
    </title> 
    <style> 
        div { 
            width:100px; 
            height:100px; 
            background:green; 
            position:relative; 
            -webkit-animation:move_1 2s infinite; 
            /* For Chrome, Safari, Opera*/ 
            animation:move_1 2s infinite; 
        } 
        /* For Chrome, Safari, Opera */ 
          
        @-webkit-keyframes move_1 { 
            from { 
                left:150px; 
            } 
            to { 
                left:350px; 
            } 
        } 
          
        @keyframes move_1 { 
            from { 
                left:150px; 
            } 
            to { 
                left:350px; 
            } 
        } 
        /* For Chrome, Safari, Opera */ 
          
        @-webkit-keyframes move_2 { 
            from { 
                left:270px; 
                width:0px; 
            } 
            to { 
                left:270px; 
                width:100px; 
                background:blue; 
            } 
        } 
          
        @keyframes move_2 { 
            from { 
                left:270px; 
                width:0px; 
            } 
            to { 
                left:270px; 
                width:100px; 
                background:blue; 
            } 
        } 
    </style> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>DOM animationName Property</h2> 
    <button onclick="Geeks()"> 
        Click Here 
    </button> 
    <br> 
    <br> 
    <div id="GFG_DIV"></div> 
  
    <script> 
        function Geeks() { 
            
            // Changing the animation name. 
            document.getElementById( 
              "GFG_DIV").style.WebkitAnimationName = 
              "move_2";  
            
            // for Chrome, Safari, and Opera 
            document.getElementById( 
              "GFG_DIV").style.animationName = "move_2"; 
        } 
    </script> 
</body> 
  
</html>

输出

  • 在单击按钮之前:
  • 单击按钮后:
  • 最后:

支持的浏览器:

  • Google Chrome:43.0、4.0 Webkit
  • Mozilla Firefox:16.0、5.0 moz
  • 边:10.0
  • Opera:30.0,15.0 Webkit
  • Safari:4.0 Webkit


相关用法


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