Css 简明教程
CSS - offset Property
CSS 简写属性 offset 使元素更容易沿特定路径移动。
The CSS shorthand property offset makes it easier for an element to animate along a certain path.
-
It has many characteristics that together comprise an offset transform.
-
With this transform, a specified point inside the element (offset-anchor) is aligned to a certain path position (offset-position) at various points along the route (offset-distance).
-
It also allows the element to be optionally rotated (offset-rotate) to follow the path’s direction.
Constituent Properties
offset 属性是以下 CSS 属性的简写:
The offset property is a shorthand for the following CSS properties:
Possible Values
offset 简写属性接受以下值列表。
The following list of values is accepted by offset shorthand property.
-
offset-anchor - Defines a point within the element that aligns with an offset position on the path.
-
offset-path - Defines the path along which the element is animated.
-
offset-distance - Defines how far along the path the element is placed.
-
offset-rotate - Optionally rotates the element to align with the direction of the path.
-
auto - All properties are reset to their default values.
Syntax
offset = [ <'offset-position'>? [ <'offset-path'> [ <'offset-distance'> || <'offset-rotate'> ]? ]? ]! [ / <'offset-anchor'> ]?
CSS offset - path value
以下示例演示了 offset 速记属性与路径值一起使用的用法。
The following example demonstrates the usage of offset shorthand property with path value.
<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 - path and auto value
以下示例演示了 offset 速记属性与路径和自动值一起使用的用法。
The following example demonstrates the usage of offset shorthand property with path and auto value.
<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 - Related Properties
下表列出了 offset 属性的相关属性:
Following table lists related properties to offset property:
Property |
Description |
Specifies the position inside an element’s box that acts as the offset path. |
|
Specifies where element should be positioned. |
|
Specifies element’s path inside its parent container. |
|
Specifies the orientation or direction of an element as it moves along the specified offset-path. |
|
link: css_offset-position.html[offset-position] |
Provide an element’s starting location along a route. |