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


HTML Style animationDuration用法及代码示例


HTML DOM中的Style animationDuration属性用于设置完成一个动画周期的时间间隔。

用法:

  • 它返回animationDuration属性。
    object.style.animationDuration
  • 它设置animationDuration属性。
    object.style.animationDuration = "time|initial|inherit"

属性值:


  • time:它用于指定动画将完成一个周期的时间长度。默认值为0,即不显示动画。
  • initial:它用于将样式animationDuration属性设置为其默认值。
  • inherit:它从其父级继承样式animationDuration属性。

供应商前缀:为了使浏览器兼容,许多Web开发人员通过使用扩展名来添加browser-specific属性,这些扩展名包括-webkit-(用于Safari,Google Chrome和Opera),-ms-(用于Internet Explorer,-moz-(用于Firefox),-o-(用于旧版本的Opera))等等。如果浏览器不支持任何扩展名,它将直接忽略它。

范例1:

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            HTML DOM Style animationDuration Property 
        </title> 
          
        <style>  
            div { 
                width:100px; 
                height:100px; 
                background:#32CD32; 
                position:relative; 
                  
                /*For Chrome, Safari, Opera browsers */ 
                /* animation name geeks */ 
                /* infinite animation iteration */ 
                -webkit-animation:geeks 5s infinite; 
                  
                animation:geeks 5s infinite;  
            } 
          
            /* Used for Chrome, Safari, Opera */ 
            @-webkit-keyframes geeks { 
                from { 
                    left:0px; 
                    top:20px; 
                } 
                to { 
                    left:300px; 
                    top:20px; 
                } 
            } 
            @keyframes geeks { 
                from { 
                    left:0px; 
                    top:20px; 
                } 
                to { 
                    left:300px; 
                    top:20px; 
                } 
            } 
        </style> 
    </head> 
      
    <body> 
        <button onclick = "myGeeks()"> 
            Click the button to speed up the duration of animation 
        </button> 
          
        <script> 
        function myGeeks() { 
                  
            /* For Chrome, Safari, and Opera browsers */ 
            document.getElementById("GFG").style.WebkitAnimationDuration = "1s"; 
            document.getElementById("GFG").style.animationDuration = "1s"; 
        } 
        </script> 
          
        <div id = "GFG"> 
            GeeksforGeeks 
        </div> 
    </body> 
</html>                    

输出:

范例2:

<!DOCTYPE html> 
<html> 
<head> 
    <title> 
        HTML DOM Style animationDuration Property 
    </title> 
      
    <style> 
        div { 
            width:100px; 
            height:100px; 
            background:#32CD32; 
            position:relative; 
              
            /* For Chrome, Safari, Opera */ 
            /* infinite animation iteration */ 
            -webkit-animation:mymove 5s infinite; 
              
            animation:mymove 5s infinite;  
        } 
  
        /* Chrome, Safari, Opera */ 
        @-webkit-keyframes mymove { 
            from { 
                left:0px;  
                background-color:white; 
            } 
            to { 
                left:200px;  
                background-color:#32CD32; 
            } 
        } 
        @keyframes myanim { 
            from { 
                left:0px;  
                background-color:white; 
            } 
            to { 
                left:200px;  
                background-color:#32CD32; 
            } 
        } 
          
    </style> 
</head> 
  
<body> 
      
    <button onclick = "myGeeks()"> 
        Click the button to speed 
        up the duration of animation 
    </button> 
  
    <script> 
        function myGeeks() { 
            document.getElementById("GFG").style.WebkitAnimationDuration  
                    = "1s";  
                  
            document.getElementById("GFG").style.animationDuration = "1s"; 
        } 
    </script> 
  
    <div id = "GFG"> 
        The animation-duration Property 
    </div> 
      
</body> 
</html>                    

输出:

支持的浏览器:下面列出了style animationDuration属性支持的浏览器:

  • Firefox 16.0、5.0 -moz-
  • Opera 30.0
  • 谷歌浏览器43.0
  • Safari 9.0


相关用法


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