HTML - DOM Style 物件 animationTimingFunction 屬性



HTML DOM Style 物件 **animationTimingFunction** 屬性指定動畫的速度曲線。速度曲線定義了在從一種 CSS 樣式更改為另一種 CSS 樣式時過渡的平滑程度。

語法

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

設定 animationTimingFunction 屬性
object.style.animationTimingFunction= "linear | ease | ease-in | ease-out | cubic-bezier(n, n, n, n) | initial | inherit";
獲取 animationTimingFunction 屬性
object.style.animationTimingFunction;

屬性值

描述
linear 它指定動畫從開始到結束以相同的速度播放。
ease 這是預設值,其中動畫開始和結束緩慢,但在中間更快。
ease-in 它指定動畫的緩慢開始。
ease-out 它指定動畫的緩慢結束。
ease-in-out 它指定動畫的緩慢開始和結束。
cubic-bezier(n, n, n, n) 您可以在 cubic-bezier 函式中指定自己的值,範圍從 0 到 1。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

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

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

以下示例說明了不同的屬性值及其對動畫的影響。

將 Timing-Function 設定為“linear”和“cubic”

此示例說明了當最初具有預設值 **ease** 時,linear 和 cubic animationTimingFunction。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object animationTimingFunction Property
    </title>
    <style>
        #animation {
            width: 100px;
            height: 100px;
            background: #04af2f;
            position: relative;
            animation: right 3s infinite;
        }
        @keyframes right {
            from {
                left: 0px;
            }
            to {
                left: 400px;
            }
        }
    </style>
</head>
<body>
    <p>The animation has ease value by default.</p>
    <p>Click to get different timing functions.</p>
    <button onclick="linear()">Linear</button>
    <button onclick="cubic()">Cubic</button>
    <div id="animation"></div>
    <script>
        function linear() {
            document.getElementById("animation")
            .style.animationTimingFunction = "linear";
        }
        function cubic() {
            document.getElementById("animation")
            .style.animationTimingFunction = 
            "cubic-bezier(0.2,0.8,0.6,0.2)";
        }
    </script>
</body>
</html>

將 Timing-Function 設定為“ease-in”、“ease-out”和“ease-in-out”

此示例說明了當最初具有預設值 **ease** 時,ease-in、ease-out 和 ease-in-out animationTimingFunction。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object animationTimingFunction Property
    </title>
    <style>
        #animation {
            width: 100px;
            height: 100px;
            background: #04af2f;
            position: relative;
            animation: right 3s infinite;
        }
        @keyframes right {
            from {
                left: 0px;
            }
            to {
                left: 400px;
            }
        }
    </style>
</head>
<body>
    <p>The animation has ease value by default.</p>
    <p>Click to get different timing functions.</p>
    <button onclick="inn()">Ease-in</button>
    <button onclick="out()">Ease-out</button>
    <button onclick="inOut()">Ease-in-out</button>
    <div id="animation"></div>
    <script>
        function inn() {
            document.getElementById("animation")
            .style.animationTimingFunction = "ease-in";
        }
        function out() {
            document.getElementById("animation")
            .style.animationTimingFunction = "ease-out";
        }
        function inOut() {
            document.getElementById("animation")
            .style.animationTimingFunction = "ease-in-out";
        }
    </script>
</body>
</html>

支援的瀏覽器

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