HTML - DOM Style 物件 animationDuration 屬性



HTML DOM Style 物件 **animationDuration** 屬性用於指定動畫完成一個迴圈所需的時間長度。它的預設值為 0。

語法

以下是獲取或設定屬性的語法。

設定屬性
object.style.animationDuration= "time | initial | inherit";
獲取屬性
object.style.animationDuration;

屬性值

此屬性接受以下列出的值。

描述
time 用於指定動畫完成一個迴圈所需的時間。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的 animation-duration 屬性。

HTML DOM Style 物件“animationDuration”屬性示例

以下示例說明了 animation duration 屬性。

設定動畫持續時間

在以下示例中,我們設定了動畫持續時間。

<!DOCTYPE html>
<html>
<head>
    <title>HTML DOM Style Object animation-Duration Property</title>
    <style>
        #animation {
            width: 100px;
            height: 100px;
            background: #04af2f;
            position: relative;
            animation: right infinite;
        }
        @keyframes right {
            from {
                left: 0px;
            }
            to {
                left: 400px;
            }
        }
    </style>
</head>
<body>
    <p>Click to change the direction of animation.</p>
    <button onclick="fun()">Click me</button>
    <div id="animation"></div>
    <script>
        function fun() {
            document.getElementById("animation").style.animationDuration = "3s";
        }
    </script>
</body>
</html>

更改動畫速度

在此示例中,我們透過改變持續時間來更改動畫速度。

<!DOCTYPE html>
<html>
<head>
    <title>HTML DOM Style Object animation-Duration Property</title>
    <style>
        #animation {
            width: 100px;
            height: 100px;
            background: #04af2f;
            position: relative;
            animation: right infinite;
        }
        @keyframes right {
            from {
                left: 0px;
            }
            to {
                left: 400px;
            }
        }
    </style>
</head>
<body>
    <p>Click to change the direction of animation.</p>
    <button onclick="fun()">Speed Up</button>
    <button onclick="funtwo()">Speed Down</button>
    <div id="animation"></div>
    <script>
        function fun() {
            document.getElementById("animation").style.animationDuration = "1s";
        }
        function funtwo() {
            document.getElementById("animation").style.animationDuration = "5s";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
animationDuration 是 43 是 12 是 16 是 9 是 30
html_dom_style_object_reference.htm
廣告