animation-range

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

animation-range 是一個 CSS 簡寫屬性,用於設定動畫在其時間軸上的附著範圍的起點和終點,即動畫在時間軸上的哪個位置開始和結束。

構成屬性

此屬性是以下 CSS 屬性的簡寫:

語法

css
/* single keyword or length percentage value */
animation-range: normal; /* Equivalent to normal normal */
animation-range: 20%; /* Equivalent to 20% normal */
animation-range: 100px; /* Equivalent to 100px normal */

/* single named timeline range value */
animation-range: cover; /* Equivalent to cover 0% cover 100% */
animation-range: contain; /* Equivalent to contain 0% contain 100% */
animation-range: cover 20%; /* Equivalent to cover 20% cover 100% */
animation-range: contain 100px; /* Equivalent to contain 100px cover 100% */

/* two values for range start and end */
animation-range: normal 25%;
animation-range: 25% normal;
animation-range: 25% 50%;
animation-range: entry exit; /* Equivalent to entry 0% exit 100% */
animation-range: cover cover 200px; /* Equivalent to cover 0% cover 200px */
animation-range: entry 10% exit; /* Equivalent to entry 10% exit 100% */
animation-range: 10% exit 90%;
animation-range: entry 10% 90%;

animation-range 簡寫屬性可以應用於容器元素,作為 <animation-range-start><animation-range-end> 值的組合。如果兩個值都指定了,它們將按 <animation-range-start> 然後 <animation-range-end> 的順序解釋。

如上面的語法塊中的註釋所示,如果只提供一個值,有幾種可能的解釋:

  • 如果該值是 <length-percentage>normal,則 <animation-range-start> 將取該值,而 <animation-range-end> 將等於 normal
  • 如果該值是一個已命名的時間軸範圍,且後面沒有 <length-percentage>,則範圍將是該已命名時間軸範圍的 0% 到 100%。
  • 如果該值是一個已命名的時間軸範圍,且後面跟著一個 <length-percentage>,則範圍將從該已命名時間軸範圍和指定的百分比開始,到該已命名時間軸範圍的 100% 結束。

一個或兩個值,表示 animation-range-start 和/或 animation-range-end。這些值可以是以下之一:

normal

對於 animation-range-start,表示時間軸的開始;對於 animation-range-end,表示時間軸的結束。這是預設值。

<length-percentage>

從時間軸開始處計算的長度或百分比值。

<timeline-range-name>

整個時間軸內的一個特定的已命名時間軸範圍。可能的值有:

cover

表示一個*已命名檢視進度時間軸*(更多細節見 CSS 滾動驅動動畫)的完整範圍,從主體元素首次開始進入滾動埠的檢視進度可見範圍(0% 進度)到它完全離開該範圍(100% 進度)的點。

contain

表示一個*已命名檢視進度時間軸*的範圍,在此範圍內,主體元素被滾動埠的檢視進度可見範圍完全包含,或完全包含該範圍。

  • 如果主體元素小於滾動埠,則範圍從主體元素首次被滾動埠完全包含(0% 進度)的點,到它不再被滾動埠完全包含(100% 進度)的點。
  • 如果主體元素大於滾動埠,則範圍從主體元素首次完全覆蓋滾動埠(0% 進度)的點,到它不再完全覆蓋滾動埠(100% 進度)的點。
entry

表示一個*已命名檢視進度時間軸*的範圍,從主體元素首次開始進入滾動埠(0% 進度),到它完全進入滾動埠(100%)的點。

exit

表示一個*已命名檢視進度時間軸*的範圍,從主體元素首次開始退出滾動埠(0% 進度),到它完全退出滾動埠(100%)的點。

entry-crossing

表示一個*已命名檢視進度時間軸*的範圍,從主體元素首次開始穿過滾動埠的起始邊緣(0% 進度),到它完全穿過滾動埠的起始邊緣(100%)的點。

exit-crossing

表示一個*已命名檢視進度時間軸*的範圍,從主體元素首次開始穿過滾動埠的結束邊緣(0% 進度),到它完全穿過滾動埠的結束邊緣(100%)的點。

對於不包含 <length-percentage><timeline-range-name> 值,如果它是 animation-range-start 值,則百分比預設為 0%;如果它是 animation-range-end 值,則預設為 100%

備註: 僅從上述描述很難想象這些值具體意味著什麼。幸運的是,檢視時間軸範圍視覺化工具以簡單直觀的方式準確地展示了它們的含義。

<timeline-range-name> <length-percentage>

一個組合值,等於指定的已命名時間軸範圍中從該時間軸範圍起點算起的指定百分比或距離。

備註: 滾動埠(更多細節見滾動容器)中被稱為檢視進度可見範圍的區域,是指*已命名檢視進度時間軸*動畫的主體元素被視為可見的區域。預設情況下,這是滾動埠的整個範圍,但可以使用 view-timeline-inset 屬性進行調整。

正式定義

初始值作為簡寫中的每個屬性
應用於所有元素
繼承性
百分比如果指定了已命名時間軸範圍,則相對於該範圍;否則相對於整個時間軸。
計算值作為簡寫中的每個屬性
  • animation-range-start:一個列表,其中每個專案可以是“normal”、一個長度百分比,或者一個時間軸範圍名稱和一個長度百分比。
  • animation-range-end:一個列表,其中每個專案可以是“normal”、一個長度百分比,或者一個時間軸範圍名稱和一個長度百分比。
動畫型別作為簡寫中的每個屬性

正式語法

animation-range = 
[ <'animation-range-start'> <'animation-range-end'>? ]#

<animation-range-start> =
[ normal | <length-percentage> | <timeline-range-name> <length-percentage>? ]#

<animation-range-end> =
[ normal | <length-percentage> | <timeline-range-name> <length-percentage>? ]#

<length-percentage> =
<length> |
<percentage>

示例

檢視時間軸範圍視覺化工具

請參閱檢視時間軸範圍視覺化工具,以獲取關於所有值型別含義的簡單直觀解釋。

建立一個帶範圍的已命名檢視進度時間軸

在類名為 animation 的主體元素上,使用 view-timeline 屬性定義了一個名為 --subject-reveal 的檢視進度時間軸。然後使用 animation-timeline: --subject-reveal; 將其設定為同一元素的時間軸。結果是,當文件被滾動時,主體元素在向上移動的過程中會播放動畫。

還設定了一個 animation-range 宣告,使動畫比預期晚開始,並提前結束。

HTML

示例的 HTML 如下所示。

html
<div class="content">
  <h1>Content</h1>

  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Risus quis varius quam
    quisque id. Et ligula ullamcorper malesuada proin libero nunc consequat
    interdum varius. Elit ullamcorper dignissim cras tincidunt lobortis feugiat
    vivamus at augue.
  </p>

  <p>
    Dolor sed viverra ipsum nunc aliquet. Sed sed risus pretium quam vulputate
    dignissim. Tortor aliquam nulla facilisi cras. A erat nam at lectus urna
    duis convallis convallis. Nibh ipsum consequat nisl vel pretium lectus.
    Sagittis aliquam malesuada bibendum arcu vitae elementum. Malesuada bibendum
    arcu vitae elementum curabitur vitae nunc sed velit.
  </p>

  <div class="subject animation"></div>

  <p>
    Adipiscing enim eu turpis egestas pretium aenean pharetra magna ac. Arcu
    cursus vitae congue mauris rhoncus aenean vel. Sit amet cursus sit amet
    dictum. Augue neque gravida in fermentum et. Gravida rutrum quisque non
    tellus orci ac auctor augue mauris. Risus quis varius quam quisque id diam
    vel quam elementum. Nibh praesent tristique magna sit amet purus gravida
    quis. Duis ultricies lacus sed turpis tincidunt id aliquet. In egestas erat
    imperdiet sed euismod nisi. Eget egestas purus viverra accumsan in nisl nisi
    scelerisque. Netus et malesuada fames ac.
  </p>
</div>

CSS

subject 元素及其包含的 content 元素僅應用了最少的樣式,文字內容則設定了一些基本的字型樣式。

css
.subject {
  width: 300px;
  height: 200px;
  margin: 0 auto;
  background-color: deeppink;
}

.content {
  width: 75%;
  max-width: 800px;
  margin: 0 auto;
}

p,
h1 {
  font-family: "Helvetica", "Arial", sans-serif;
}

h1 {
  font-size: 3rem;
}

p {
  font-size: 1.5rem;
  line-height: 1.5;
}

類名為 subject<div> 也被賦予了 animation 類——這裡設定了 view-timeline 來定義一個已命名檢視進度時間軸。它還被賦予了同名的 animation-timeline,以宣告它將是隨著檢視進度時間軸推進而播放動畫的元素。我們還為它設定了一個 animation-range 宣告,使動畫比預期晚開始,並提前結束。

最後,在元素上指定了一個動畫,該動畫使其不透明度和縮放發生變化,從而在它向上移動捲軸時淡入並變大。

css
.animation {
  view-timeline: --subject-reveal block;
  animation-timeline: --subject-reveal;

  animation-name: appear;
  animation-range: entry 10% contain 25%;
  animation-fill-mode: both;
  animation-duration: 1ms; /* Firefox requires this to apply the animation */
}

@keyframes appear {
  from {
    opacity: 0;
    transform: scaleX(0);
  }

  to {
    opacity: 1;
    transform: scaleX(1);
  }
}

結果

滾動以檢視主體元素的動畫效果。

規範

規範
滾動驅動動畫
# animation-range

瀏覽器相容性

另見