複雜動畫簡介
重要事項:本文件不適用於您目前選擇的故事格式!
對於無法透過新增和移除類別來驅動的動畫,AMP 提供了幾個動畫專用元件。這些元件將 AMP 的原則應用於動畫:它們快速、高效且以使用者優先。AMP 限制了關鍵影格內允許的 CSS 屬性,但提供了諸如精細控制、無縫動畫和跨瀏覽器相容性等優點,而且無需額外的工作。
如果您需要嚴格控制播放,以及在多個元素同時動畫化時具有精確的計時,請使用 amp-animation。
建立基礎 AMP 動畫
amp-animation 元件可在 AMP 中使用 Web Animation API。
基礎的 amp-animation 是一個由以下關鍵部分組成的 JSON 物件
<amp-animation layout="nodisplay" id="exampleAnimation">
<script type="application/json">
{
"selector": "#elementID", //select the element to animate
"duration": "1s", //timing property
"iterations": 2, //timing property
"fill": "both", //timing property
"keyframes": {"opacity": 0, "transform": "scale(2)"} //keyframes
}
</script>
</amp-animation>
<!-- trigger -->
<button on="tap:exampleAnimation.start">
選擇器
就像 CSS 一樣,amp-animation 元件透過在 "selector" 欄位中宣告元素的標籤名稱、類別或 ID,將動畫屬性連結到元素。該元件會動畫化每個具有已宣告標籤類型或類別名稱的元素。使用 ID 可確保您只動畫化單一元素。
時間屬性
時間屬性控制動畫持續時間、播放次數以及關鍵影格執行的方向。
不需要時間屬性,但如果缺少與時間和顯示相關的屬性(例如 duration 和 fill),動畫可能無法執行。
關鍵影格
雖然 CSS 允許您透過轉場效果從一種狀態變形到另一種狀態,但您必須將動畫屬性宣告為關鍵影格才能實作 amp-animation。amp-animation 可用於 GPU 加速屬性,這些屬性不會導致重新排版,並且可以在合成器執行緒上進行動畫處理。這可防止動畫干擾 AMP 和瀏覽器的渲染程序。
觸發器
觸發器啟動動畫序列。amp-animation 擴充功能會在頁面上的 body 變成可見時啟動,或者透過將其連接到 AMP 動作或事件來啟動。
當動畫應在頁面載入後立即執行時,觸發 body 的可見性非常有用,因為它會顯示在「首頁上方」或頁面的第一個可視區域內。動畫透過可見性觸發,方法是在元件中新增 trigger="visibility" 作為屬性。
<amp-animation layout="nodisplay"
trigger="visibility">
...
</amp-animation>
動畫透過為 amp-animation 元件指派 ID 並將該 ID 連結到所需的事件觸發器(例如點擊按鈕)來連接到動作或事件。
<amp-animation layout="nodisplay" id="exampleAnimation">
...
</amp-animation>
<button on="tap:exampleAnimation.start">
建構複雜動畫
在 amp-animation 中建構動畫可實現精細的控制,而不僅僅是啟動和停止動畫:它還可以暫停、反轉和導向特定點。您甚至可以將多個動畫鏈接在一起,並按順序動畫化元素。
子目標
相同標籤或類別的元素可以具有指定的時間屬性,並覆寫在頂層動畫中定義的變數值。
<body>
<h1>Hello World!</h1>
<h1>Hello World!</h1>
<h1 id="helloMe">Hello World!</h1>
<h1>Hello World!</h1>
<amp-animation layout="nodisplay" id="animateThis">
<script type="application/json">
{
"selector": "h1",
"duration": "3s",
"fill": "both",
"keyframes": [
{"transform": "translateX(0px)"},
{"transform": "translateX(50%)"}
],
"subtargets": [
{
"index": 1,
"duration": "1s"
},
{
"selector": "#helloMe",
"direction": "reverse",
"duration": "5s"
}
]
}
</script>
</amp-animation>
<button on="tap:animateThis.start">start</button>
</body>
鏈式動畫
多個動畫可以連接在一起以形成一個大型序列。您可以透過在 amp-animation 元件內的 animations 陣列中編寫動畫來建立定時效果,例如影片上的覆疊。
<amp-animation id="overlaysAnim" layout="nodisplay">
<script type="application/json">
{
"duration": "3s",
"fill": "both",
"animations": [{
"selector": ".one",
"keyframes": [{
"opacity": "1",
"offset": 0
},
{
"opacity": "1",
"offset": 0.04
},
{
"opacity": "0",
"offset": 0.0401
},
{
"opacity": "0",
"offset": 1
}
]
},
]
}
</script>
</amp-animation>
此設定會依序播放每個動畫 3 秒。
對於較大的動畫,animations 陣列內的動畫能夠參考其他 amp-animation 元件。
<amp-animation id="addEnergy" layout="nodisplay">
<script type="application/json">
{
"duration": "0.3s",
"fill": "both",
"direction": "alternate",
"animations": [
{
"selector": "#energy",
"keyframes": [
{"transform": "scaleX(calc(num(width('#energy'))/10))"},
{"transform": "scaleX(calc(num(width('#energy'))/10 + 3))"}
]
},
{
"animation": "atomExcite"
}
]
}
</script>
</amp-animation>
<amp-animation id="atomExcite" layout="nodisplay" trigger="visibility">
<script type="application/json">
{
"duration": "0.3s",
"iterations": "2",
"fill": "both",
"direction": "alternate",
"animations": [
{
"selector": ".atom",
"keyframes": {
"transform": "translate(20vw)"
}
}
]
}
</script>
</amp-animation>
動畫化未知數量的元素
透過使用 [`var(/content/amp-dev/documentation/components/reference/amp-animation.md#css-extensions), 您可以編寫可處理任何數量元素的複雜且定時的動畫。這允許輕鬆流暢地動畫化動態和使用者產生的資料。
<head>
<script
async
custom-element="amp-animation"
src="https://cdn.ampproject.org/v0/amp-animation-0.1.js"
></script>
<style amp-custom>
.parent {
perspective: 1000px;
transform-style: preserve-3d;
position: relative;
margin: 10px;
width: 239px;
height: 335px;
}
.card {
transform-origin: left;
height: 50%;
width: 50%;
}
</style>
</head>
<body>
<amp-animation layout="nodisplay" id="cardAdmin">
<script type="application/json">
{
"selector": ".card",
"--duration": "2s",
"duration": "var(--duration)",
"delay": "calc((length() - index() - 1) * var(--duration))",
"easing": "ease-in",
"iterations": "1",
"fill": "both",
"keyframes": [
{"transform": "translate3d(0px, 0px, 0px)"},
{"transform": "translate3d(50%, 0px, 100px)"},
{"transform": "translate3d(110%, 0px, 0px) rotateY(-20deg)"},
{"transform": "translate3d(50%, 0px, -100px)"},
{"transform": "translate3d(0px, 0px, -1px)"}
]
}
</script>
</amp-animation>
<div class="parent" on="tap:cardAdmin.start" tabindex="none" role="animation">
<amp-img
class="card"
src="https://upload.wikimedia.org/wikipedia/commons/7/70/3C.svg"
layout="fill"
></amp-img>
<amp-img
class="card"
src="https://upload.wikimedia.org/wikipedia/commons/3/3a/3H.svg"
layout="fill"
></amp-img>
<amp-img
class="card"
src="https://upload.wikimedia.org/wikipedia/commons/e/e1/KC.svg"
layout="fill"
></amp-img>
</div>
</body>
此範例的運作方式如下:
- 宣告一個變數 --duration,並將其值設為兩秒。
- 將 duration 設定為變數 --duration 的值。
- 計算套用至每個類別為 .card 的元素的延遲。 1. length(/content/amp-dev/documentation/components/reference/amp-animation.md#css-length()-extension) 會計算已選取多少個 .card 元素 1. 然後 length 會減去每個 .card 的 index(/content/amp-dev/documentation/components/reference/amp-animation.md#css-index()-extension) 1. 將結果值乘以變數 --duration 1. 最後的總計會以秒為單位套用至該元素的延遲
- 動畫會個別套用至每個元素,以便卡片依序洗牌,而不是同時全部洗牌。
在 AMP playground 中開啟動畫,並新增更多 amp-img 元素以測試此行為。
在任何地方都看起來很棒
動畫可以包含條件。
<head>
<style amp-custom>
.drop {
width: 20px;
height: 20px;
background: blue;
margin-top: 1em;
border-radius: 50%;
}
.right {
position: absolute;
right: 0;
background: red;
}
</style>
<script
async
custom-element="amp-animation"
src="https://cdn.ampproject.org/v0/amp-animation-0.1.js"
></script>
</head>
<body>
<amp-animation id="mediaAnimation" layout="nodisplay">
<script type="application/json">
{
"duration": "1s",
"iterations": "4",
"fill": "both",
"direction": "alternate",
"animations": [
{
"media": "(min-width: 300px)",
"selector": ".drop",
"keyframes": {
"transform": "translate(100vw)"
}
},
{
"media": "(max-width: 300px)",
"selector": ".drop",
"keyframes": {
"transform": "translate(50vw)"
}
},
{
"media": "(min-width: 300px)",
"selector": ".right",
"keyframes": {
"transform": "translate(-100vw)"
}
},
{
"media": "(max-width: 300px)",
"selector": ".right",
"keyframes": {
"transform": "translate(-50vw)"
}
}
]
}
</script>
</amp-animation>
<div class="rain">
<div class="drop"></div>
<div class="drop right"></div>
<div class="drop"></div>
<div class="drop right"></div>
<div class="drop"></div>
<div class="drop right"></div>
<div class="drop"></div>
<div class="drop right"></div>
</div>
<button on="tap:mediaAnimation.start">Start</button>
</body>
-
由 @CrystalOnScript 撰寫