CSS 動畫 for HyperFrames
HyperFrames 可透過其 css 執行時適配器搜尋 CSS 關鍵影格動畫。這適用於簡單的重複圖案、背景運動、微光、發光、遮罩及非序列裝飾。
對於場景編排,通常 GSAP 更清晰。CSS 動畫最適合屬於單一元素且具有固定持續時間的運動。
契約
- 在執行時初始化完成前,將動畫元素放入 DOM 中。
- 為帶有時間的元素設定
data-start值,使本地動畫時間與剪輯時間一致。 - 使用有限的
animation-duration和animation-iteration-count,因為在沒有 WAAPI 支援 CSS 動畫的環境中,負延遲無法表示無限持續時間。 - 偏好使用
animation-fill-mode: both,使搜尋狀態在主動運動前後保持。 - 避免使用依賴時脈的 JavaScript、懸停觸發的狀態以及依賴使用者事件的類別切換。
適配器會發現具有計算 animation-name 的元素,在可用時尋找其瀏覽器 Animation 控制代碼,並回退到使用負 animation-delay 進行暫停。
基本模式
<div
id="pulse-ring"
class="clip pulse-ring"
data-start="0"
data-duration="4"
data-track-index="2"
></div>
<style>
.pulse-ring {
width: 280px;
height: 280px;
border: 4px solid rgba(255, 255, 255, 0.7);
border-radius: 50%;
animation-name: pulse-ring;
animation-duration: 1200ms;
animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
animation-iteration-count: 3;
animation-fill-mode: both;
}
@keyframes pulse-ring {
from {
opacity: 0;
transform: scale(0.82);
}
35% {
opacity: 1;
}
to {
opacity: 0;
transform: scale(1.18);
}
}
</style>
交錯模式
使用 CSS 自定義屬性以避免重複關鍵影格:
<div class="clip dots" data-start="1" data-duration="3" data-track-index="3">
<span style="--i: 0"></span>
<span style="--i: 1"></span>
<span style="--i: 2"></span>
</div>
<style>
.dots span {
display: inline-block;
width: 18px;
height: 18px;
margin-right: 10px;
border-radius: 50%;
background: currentColor;
animation: dot-pop 900ms ease-out both;
animation-delay: calc(var(--i) * 120ms);
}
@keyframes dot-pop {
from {
opacity: 0;
transform: translateY(18px) scale(0.75);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
</style>
良好用途
- 具有已知重複次數的裝飾性循環。
- 遮罩、發光、微光、顆粒及微妙的視差層。
- 簡單的單一元素入場,其中完整的 JS 時間軸顯得多餘。
應避免
- 無限 CSS 動畫,除非您已確認瀏覽器暴露了可搜尋的 WAAPI 支援的 CSS 動畫控制代碼。偏好使用覆蓋可見持續時間的有限迭代次數。
- 在可使用變換時,對佈局屬性(如
top、left、width或height)進行動畫。 - 依賴懸停、焦點、滾動或媒體查詢來觸發渲染關鍵的運動。
- 在啟動後更改動畫類別,除非另一個確定性時間軸控制該更改。
驗證
編輯 CSS 動畫組合後:
npx hyperframes lint
npx hyperframes validate
致謝與參考
- HyperFrames 適配器原始碼:
packages/core/src/runtime/adapters/css.ts。 - MDN CSS 動畫文件:https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/animation
- MDN
animation-fill-mode:https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode