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 提供元素沿路线的起始位置。
广告

© . All rights reserved.