offset-position
offset-position CSS 属性定义了元素沿路径的初始位置。此属性通常与 offset-path 属性结合使用,以创建运动效果。如果 offset-path 函数(例如 path())未指定其自己的起始位置,则 offset-position 的值将决定元素在沿偏移路径移动时的初始放置位置。
offset-position 属性是基于 offset 组成属性的运动系统的一部分,其中包括 offset-anchor、offset-distance 和 offset-path。这些属性协同工作,以沿路径创建各种运动效果。
语法
/* Keyword values */
offset-position: normal;
offset-position: auto;
offset-position: top;
offset-position: bottom;
offset-position: left;
offset-position: right;
offset-position: center;
/* <percentage> values */
offset-position: 25% 75%;
/* <length> values */
offset-position: 0 0;
offset-position: 1cm 2cm;
offset-position: 10ch 8em;
/* Edge offsets values */
offset-position: bottom 10px right 20px;
offset-position: right 3em bottom 10px;
offset-position: bottom 10px right;
offset-position: top right 10px;
/* Global values */
offset-position: inherit;
offset-position: initial;
offset-position: revert;
offset-position: revert-layer;
offset-position: unset;
值
normal-
表示元素没有偏移起始位置,并将元素放置在包含块的
50% 50%处。这是默认值。 auto-
表示偏移起始位置是元素框的左上角。
<position>-
将位置指定为 x/y 坐标,以相对于其框边缘放置元素。位置可以使用一到四个值定义。如果使用两个非关键字值,则第一个值表示水平位置,第二个值表示垂直位置。如果只指定一个值,则假定第二个值为
center。如果使用三个或四个值,则<length-percentage>值是前导关键字值的偏移量。有关这些值类型的更多解释,请参阅background-position。
正式定义
正式语法
offset-position =
normal |
auto |
<position>
<position> =
<position-one> |
<position-two> |
<position-four>
<position-one> =
left |
center |
right |
top |
bottom |
x-start |
x-end |
y-start |
y-end |
block-start |
block-end |
inline-start |
inline-end |
<length-percentage>
<position-two> =
[ left | center | right | x-start | x-end ] && [ top | center | bottom | y-start | y-end ] |
[ left | center | right | x-start | x-end | <length-percentage> ] [ top | center | bottom | y-start | y-end | <length-percentage> ] |
[ block-start | center | block-end ] && [ inline-start | center | inline-end ] |
[ start | center | end ]{2}
<position-four> =
[ [ left | right | x-start | x-end ] <length-percentage> ] && [ [ top | bottom | y-start | y-end ] <length-percentage> ] |
[ [ block-start | block-end ] <length-percentage> ] && [ [ inline-start | inline-end ] <length-percentage> ] |
[ [ start | end ] <length-percentage> ]{2}
<length-percentage> =
<length> |
<percentage>
示例
设置 offset-path 的初始 offset-position
在此示例中,offset-path 属性用于定义 cyan 元素应沿其移动的路径。path() CSS 函数的值是描述弯曲路径的 SVG 数据路径。元素在 move 动画期间沿此弯曲路径移动。
HTML
<div id="wrap">
<div id="motion-demo"></div>
</div>
CSS
#motion-demo {
offset-path: path("M20,20 C20,100 200,0 200,100");
offset-position: left top;
animation: move 3000ms infinite alternate ease-in-out;
width: 40px;
height: 40px;
background: cyan;
}
@keyframes move {
0%,
20% {
offset-distance: 0%;
}
80%,
100% {
offset-distance: 100%;
}
}
结果
比较各种偏移起始位置
此示例直观地比较了当使用 ray() 为 offset-path 属性指定值时,元素的各种初始偏移起始位置。元素框内的数字表示应用了 CSS 的元素以及元素的锚点。
.box {
background-color: green;
border-top: 6px dashed white;
background-clip: border-box;
position: absolute;
top: 20px;
left: 20px;
opacity: 20%;
color: white;
}
.box0 {
offset-position: normal;
}
.box1 {
offset-position: normal;
offset-path: ray(0deg);
}
.box2 {
offset-position: auto;
offset-path: ray(0deg);
}
.box3 {
offset-position: left top;
offset-path: ray(0deg);
}
.box4 {
offset-position: 30% 70%;
offset-path: ray(120deg);
}
结果
在 box0 中,缺少 offset-path 属性意味着 normal 或 auto 的 offset-position 无效。当 offset-position 为 normal 时,射线从包含块的中心(即 50% 50%)开始。这是偏移路径的默认起始位置,当未指定 offset-position 时使用。请注意偏移起始位置 auto 和 left top 之间的区别。值 auto 将元素的锚点与其自身的左上角对齐,而值 left top 将元素的锚点与包含块的左上角对齐。
规范
| 规范 |
|---|
| Motion Path Module Level 1 # offset-position-property |
浏览器兼容性
加载中…