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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。