HTML - DOM樣式物件animationDirection屬性



HTML DOM 樣式物件**animationDirection**屬性設定動畫的方向。其預設值為normal。要使此屬性生效,動畫設定不能設定為只播放一次。

語法

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

設定屬性
object.style.animationDirection = "normal | reverse | alternate | alternate-reverse | initial | inherit";
獲取屬性
object.style.animationDirection;

屬性值

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

描述
normal 此屬性值按正常方式播放動畫,即向前播放。
reverse 它反向播放動畫。
alternate 它交替播放正向和反向動畫,奇數次為正向,偶數次為反向。
alternate-reverse 它是alternate的反向。動畫先反向播放,然後正向播放。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

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

HTML DOM 樣式物件“animationDirection”屬性示例

以下示例演示了animation-direction屬性。

將動畫方向設定為正常和反向

在以下示例中,我們將動畫方向設定為normal和reverse。

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

將動畫方向設定為alternate和alternate-reverse

在以下示例中,我們將動畫方向首先設定為alternate,然後設定為alternate-reverse。

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

支援的瀏覽器

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