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


HTML Style animationDelay用法及代碼示例


HTML DOM中的animationDelay屬性用於設置或返回動畫應開始的延遲。

用法:

  • 它用於設置animationDelay屬性:
    object.style.animationDelay = "time|initial|inherit"
  • 它用於返回animationDelay屬性:
    object.style.animationDelay

屬性值:


  • time:它用於定義動畫延遲的秒數或毫秒數。預設值為0。
  • initial:用於將屬性設置為其默認值。
  • inherit:它用於從其父級繼承屬性。

示例1:使用時間屬性值在動畫中延遲3秒。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM animationDelay 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 animationDelay 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"; 
            
            // Changing the animation delay. 
            document.getElementById( 
              "GFG_DIV").style.WebkitAnimationDelay =  
              "3s"; 
            
            // for Chrome, Safari, and Opera 
            document.getElementById( 
              "GFG_DIV").style.animationDelay =  
              "3s"; 
        } 
    </script> 
</body> 
  
</html>

輸出

  • 在單擊按鈕之前:
  • 單擊按鈕後:
  • 最後:

示例2:動畫延遲3秒。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM animationDelay 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 animationDelay 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"; 
            
            // Changing the animation Delay. 
            document.getElementById( 
              "GFG_DIV").style.WebkitAnimationDelay =  
              "3s";  
            
             // for Chrome, Safari, and Opera 
            document.getElementById( 
              "GFG_DIV").style.animationDelay = 
              "3s"; 
        } 
    </script> 
</body> 
  
</html>

輸出

  • 在單擊按鈕之前:
  • 單擊按鈕後:
  • 最後:

支持的瀏覽器:下麵列出了DOM Style animationDelay屬性支持的瀏覽器:

  • 穀歌瀏覽器:43.0
  • Mozilla Firefox:16.0、5.0 moz
  • Opera :30.0
  • Safari:9.0


相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 HTML | DOM Style animationDelay Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。