CSS - offset 屬性



CSS 簡寫屬性offset使元素沿特定路徑動畫變得更容易。

  • 它具有許多共同構成偏移變換的特性。

  • 透過此變換,元素內部的指定點(offset-anchor)在路線上的各個點與特定路徑位置(offset-position)對齊(offset-distance)。

  • 它還允許元素可選地旋轉(offset-rotate)以遵循路徑的方向。

組成屬性

offset屬性是以下 CSS 屬性的簡寫

可能的值

offset簡寫屬性接受以下值列表。

  • offset-anchor - 定義元素內與路徑上的偏移位置對齊的點。

  • offset-path - 定義元素沿其進行動畫的路徑。

  • offset-distance - 定義元素沿路徑放置的位置。

  • offset-rotate - 可選地旋轉元素以與路徑的方向對齊。

  • auto - 所有屬性都重置為其預設值。

應用於

可轉換元素

語法

offset = [ <'offset-position'>? [ <'offset-path'> [ <'offset-distance'> || <'offset-rotate'> ]? ]? ]! [ / <'offset-anchor'> ]?    

CSS offset - 路徑值

以下示例演示了使用帶路徑值的offset簡寫屬性。

       
<html>
<head>
<style>
    @keyframes slide {
        0% {
            offset-distance: 0%;
        }
        100% {
            offset-distance: 100%;
        }
    }
    .container {
        width: 400px;
        height: 200px;
        border: 2px solid #3498db;
        border-radius: 10px;
        position: relative;
        overflow: hidden;
        background-color: #f0f0f0;
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
    }
    .text {
        position: absolute;
        font-size: 28px;
        color: #3954cc;
        animation: slide 6s ease-in-out infinite alternate;
        offset: path('M 10 100 Q 50 50 90 100 T 170 100 T 250 100');
        text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.4);
    }
</style>
</head>
<body>
<div class="container">
    <div class="text">This is Sliding Text</div>
</div>
</body>
</html>

CSS offset - 路徑和自動值

以下示例演示了使用帶路徑和自動值的offset簡寫屬性。

<html>
<head>
<style>
    @keyframes orbit {
        0% {
            offset-distance: 0%;
            offset-rotate: 0deg;
        }
        100% {
            offset-distance: 100%;
            offset-rotate: 360deg;
        }
    }
    #planet {
        width: 60px;
        height: 60px;
        background-color: #0000A0;
        border-radius: 50%;
        position: absolute;
        animation: orbit 6s linear infinite;
        offset: path('M 200 200 m -100, 0 a 100,100 0 1,0 200,0 a 100,100 0 1,0 -200,0') auto;
    }
    #sun {
        width: 100px;
        height: 100px;
        background-color: #ffd700;
        border-radius: 50%;
        position: absolute;
        left: 28%;
        top: 33%;
        transform: translate(-50%, -50%);
    }
</style>
</head>
<body>
<div id="sun"></div>
<div id="planet"></div>
</body>
</html>

CSS 偏移 - 相關屬性

下表列出了與offset屬性相關的屬性

屬性 描述
offset-anchor 指定元素框內充當偏移路徑的位置。
offset-distance 指定元素應放置的位置。
offset-path 指定元素在其父容器內的路徑。
offset-rotate 指定元素在沿指定的 offset-path 移動時的方向。
offset-position 提供元素沿路線的起始位置。
廣告