當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。