offset-anchor

Baseline 2023
新推出

自 2023 年 8 月起,此功能已在最新的裝置和瀏覽器版本中可用。此功能可能不適用於舊裝置或瀏覽器。

offset-anchor CSS 屬性指定了沿 offset-path 移動的元素框內的實際移動點。

試一試

offset-anchor: auto;
offset-anchor: right top;
offset-anchor: left bottom;
offset-anchor: 20% 80%;
<section class="default-example" id="default-example">
  <div class="wrapper">
    <div id="example-element"></div>
  </div>
  <button id="playback" type="button">Play</button>
</section>
#example-element {
  offset-path: path("M 0,20 L 200,20");
  animation: distance 3000ms infinite alternate ease-in-out;
  width: 40px;
  height: 40px;
  background: cyan;
  animation-play-state: paused;
}

#example-element.running {
  animation-play-state: running;
}

.wrapper {
  background-image: linear-gradient(
    to bottom,
    transparent,
    transparent 49%,
    black 50%,
    black 51%,
    transparent 52%
  );
  border: 1px solid #cccccc;
  width: 90%;
}

@keyframes distance {
  0% {
    offset-distance: 0%;
  }
  100% {
    offset-distance: 100%;
  }
}

#playback {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 1em;
}
const example = document.getElementById("example-element");
const button = document.getElementById("playback");

button.addEventListener("click", () => {
  if (example.classList.contains("running")) {
    example.classList.remove("running");
    button.textContent = "Play";
  } else {
    example.classList.add("running");
    button.textContent = "Pause";
  }
});

語法

css
/* Keyword values */
offset-anchor: top;
offset-anchor: bottom;
offset-anchor: left;
offset-anchor: right;
offset-anchor: center;
offset-anchor: auto;

/* <percentage> values */
offset-anchor: 25% 75%;

/* <length> values */
offset-anchor: 0 0;
offset-anchor: 1cm 2cm;
offset-anchor: 10ch 8em;

/* Edge offsets values */
offset-anchor: bottom 10px right 20px;
offset-anchor: right 3em bottom 10px;

/* Global values */
offset-anchor: inherit;
offset-anchor: initial;
offset-anchor: revert;
offset-anchor: revert-layer;
offset-anchor: unset;

auto

offset-anchor 的值與元素的 transform-origin 相同,除非 offset-pathnone,在這種情況下它會從 offset-position 獲取值。

<position>

<position> 定義了一個 x/y 座標,用於將專案相對於元素框的邊緣放置。它可以由一到四個值定義。有關更多詳細資訊,請參閱 <position>background-position 參考頁面。請注意,3 值位置語法不適用於任何 <position> 的用法,除了 background(-position) 中。

正式定義

初始值auto
應用於可變換元素
繼承性
百分比相對於元素參考框的寬度和高度
計算值對於 <length> 則是絕對值,否則為百分比
動畫型別一個 position

正式語法

offset-anchor = 
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-anchor 值

在以下示例中,我們有三個巢狀在 <section> 元素中的 <div> 元素。每個 <div> 都被賦予相同的 offset-path(一條 200 畫素長的水平線),並被動畫化以沿其移動。然後,這三個被賦予不同的 background-coloroffset-anchor 值。

每個 <section> 都用線性漸變進行了樣式設定,以在其中心顯示一條水平線,從而直觀地顯示 <div> 的偏移路徑在哪裡執行。

這使您可以檢視不同 offset-anchor 值的影響——第一個,auto,導致 <div> 的中心點沿路徑移動。另外兩個分別導致 <div> 的右上角和左下角點沿路徑移動。

HTML

html
<section>
  <div class="offset-anchor1"></div>
</section>
<section>
  <div class="offset-anchor2"></div>
</section>
<section>
  <div class="offset-anchor3"></div>
</section>

CSS

css
div {
  offset-path: path("M 0,20 L 200,20");
  animation: move 3000ms infinite alternate ease-in-out;
  width: 40px;
  height: 40px;
}

section {
  background-image: linear-gradient(
    to bottom,
    transparent,
    transparent 49%,
    black 50%,
    black 51%,
    transparent 52%
  );
  border: 1px solid #cccccc;
  margin-bottom: 10px;
}

.offset-anchor1 {
  offset-anchor: auto;
  background: cyan;
}

.offset-anchor2 {
  offset-anchor: right top;
  background: purple;
}

.offset-anchor3 {
  offset-anchor: left bottom;
  background: magenta;
}

@keyframes move {
  0% {
    offset-distance: 0%;
  }
  100% {
    offset-distance: 100%;
  }
}

結果

規範

規範
Motion Path Module Level 1
# offset-anchor-property

瀏覽器相容性

另見