動態原則
基礎。每次創作技能調用時都會載入。 此處為精簡規則,深入探討請參閱
references/。
時間規則
| 情境 | 持續時間 | 原因 |
|---|---|---|
| 微互動(切換、懸停、焦點) | 100-150ms | 即時反饋,無感知延遲 |
| UI 轉換(模態框、抽屜、分頁切換) | 200-300ms | 流暢但不遲緩 |
| 頁面/路由轉換 | 300-500ms | 建立空間敘事 |
| 滾動驅動 / 3D | 自由(基於進度) | 與使用者輸入綁定,無固定持續時間 |
頻率規則: 動畫播放越頻繁,就必須越短暫且越細微。 按鈕懸停(每天 1000 次)= 100ms 透明度。引導畫面揭示(僅一次)= 600ms 以上的完整編排。
緩動速查表
| 動作 | 緩動 | 原因 |
|---|---|---|
| 元素進入 | ease-out / spring | 減速進入靜止位置(自然到達) |
| 元素離開 | ease-in | 加速離開(讓路) |
| 元素在狀態間移動 | ease-in-out | 平滑啟動與停止 |
| 滾動同步 | linear / none | 與輸入 1:1 匹配,無延遲感 |
| 彈跳/趣味 | spring(欠阻尼) | 超調帶來生命力 |
| 敏捷 UI | cubic-bezier(0.2, 0, 0, 1) | 快速啟動,平穩著陸 |
離場永遠比進場更細微。 進場:300ms ease-out,完整編排。離場:200ms ease-in,僅透明度。
原生緩動等效(跨平台)
| 網頁(CSS / JS) | SwiftUI | Compose |
|---|---|---|
cubic-bezier(0.2, 0, 0, 1) | .spring(response: 0.4, dampingFraction: 0.85) 或 .snappy | spring(stiffness = Spring.StiffnessMedium, dampingRatio = 0.85f) |
ease-out | .easeOut(duration: 0.3) | tween(durationMillis = 300, easing = LinearOutSlowInEasing) |
ease-in | .easeIn(duration: 0.2) | tween(durationMillis = 200, easing = FastOutLinearInEasing) |
| spring(彈跳) | .bouncy(iOS 17+) | spring(stiffness = Spring.StiffnessLow, dampingRatio = Spring.DampingRatioMediumBouncy) |
| spring(平滑) | .smooth(iOS 17+) | spring(stiffness = Spring.StiffnessMedium, dampingRatio = Spring.DampingRatioNoBouncy) |
無障礙(不可妥協)
減少動畫 - 強制執行
每個動畫元件都必須尊重使用者減少動畫的偏好。沒有例外,無論平台為何。
| 平台 | API |
|---|---|
| 網頁 CSS | @media (prefers-reduced-motion: reduce) |
| 網頁 JS | window.matchMedia('(prefers-reduced-motion: reduce)') |
| SwiftUI | @Environment(\.accessibilityReduceMotion) var reduceMotion |
| UIKit | UIAccessibility.isReduceMotionEnabled(+ reduceMotionStatusDidChangeNotification) |
| Compose | 使用 Settings.Global.ANIMATOR_DURATION_SCALE 的自訂輔助函式(深入探討請參閱 mobile-principles/references/accessibility-mobile.md) |
CSS:
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
SwiftUI:
struct AnimatedView: View {
@Environment(\.accessibilityReduceMotion) var reduceMotion
@State var visible = false
var body: some View {
Text("Hello")
.opacity(visible ? 1 : 0)
.animation(reduceMotion ? .none : .spring(response: 0.4, dampingFraction: 0.85), value: visible)
}
}
Compose:
@Composable
fun rememberReduceMotion(): Boolean {
val context = LocalContext.current
return remember {
Settings.Global.getFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f) == 0f
}
}
@Composable
fun AnimatedComponent(visible: Boolean) {
val reduce = rememberReduceMotion()
val alpha by animateFloatAsState(
targetValue = if (visible) 1f else 0f,
animationSpec = if (reduce) snap() else spring(stiffness = Spring.StiffnessMedium)
)
}
其他無障礙需求
- 焦點指示器絕不能被動畫隱藏
- 動畫內容必須在每一幀都符合 WCAG 對比度比率(過渡中途不允許文字淡出至不可見)
- 循環動畫:必須提供暫停機制
通用「禁止」規則
1. 永遠不要對 width/height/top/left 做動畫
每一幀都會觸發佈局重新計算 = 卡頓。
/* 錯誤 */
.drawer { transition: height 0.3s ease; }
.drawer.open { height: 400px; }
/* 正確 */
.drawer { transition: transform 0.3s ease-out; transform: translateY(100%); }
.drawer.open { transform: translateY(0); }
SwiftUI 等效(錯誤 vs 正確):
// 錯誤 - 對 frame 做動畫(引起佈局傳遞)
.frame(height: open ? 400 : 0)
.animation(.easeInOut, value: open)
// 正確 - 透過 offset 對 transform 做動畫
.offset(y: open ? 0 : 400)
.animation(.spring(), value: open)
Compose 等效(錯誤 vs 正確):
// 錯誤 - 對尺寸做動畫(完整佈局傳遞)
val height by animateDpAsState(if (open) 400.dp else 0.dp)
// 正確 - 透過 graphicsLayer 對 Y 偏移做動畫
val translation by animateFloatAsState(if (open) 0f else 400f)
Box(modifier = Modifier.graphicsLayer { translationY = translation })
2. 永遠不要縮放至 0
scale(0) 會讓元素像掉進黑洞般消失。務必保留最小值。
/* 錯誤 */
.modal-exit { transform: scale(0); }
/* 正確 */
.modal-exit { transform: scale(0.95); opacity: 0; }
SwiftUI 等效:
// 錯誤
.scaleEffect(visible ? 1.0 : 0.0)
// 正確
.scaleEffect(visible ? 1.0 : 0.95)
.opacity(visible ? 1.0 : 0.0)
Compose 等效:
// 錯誤
Modifier.graphicsLayer { scaleX = if (visible) 1f else 0f; scaleY = if (visible) 1f else 0f }
// 正確
Modifier.graphicsLayer {
scaleX = if (visible) 1f else 0.95f
scaleY = if (visible) 1f else 0.95f
alpha = if (visible) 1f else 0f
}
3. 進場時永遠不要使用 ease-in
Ease-in = 慢速啟動。進場的元素猶豫不決會讓人感覺故障。
/* 錯誤 */
.card-enter { animation: fadeIn 0.3s ease-in; }
/* 正確 */
.card-enter { animation: fadeIn 0.3s ease-out; }
/* 或透過 JS 使用 spring 來獲得自然感 */
4. UI 互動永遠不要超過 500ms
模態框、下拉選單、工具提示、分頁——使用者在等待。請尊重他們的時間。
// 錯誤
gsap.to(modal, { opacity: 1, y: 0, duration: 0.8 });
// 正確
gsap.to(modal, { opacity: 1, y: 0, duration: 0.25, ease: "power2.out" });
SwiftUI:
// 錯誤
.animation(.easeInOut(duration: 0.8), value: state)
// 正確
.animation(.spring(response: 0.25, dampingFraction: 0.85), value: state)
Compose:
// 錯誤
animateContentSize(animationSpec = tween(800))
// 正確
animateContentSize(animationSpec = spring(stiffness = Spring.StiffnessMediumLow))
5. 永遠不要忽略 prefers-reduced-motion
這是無障礙需求,不是可有可無的功能。
// 錯誤
gsap.from('.hero-title', { opacity: 0, y: 40, duration: 0.6 });
// 正確
const prefersReduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!prefersReduced) {
gsap.from('.hero-title', { opacity: 0, y: 40, duration: 0.6 });
}
請參閱上方跨平台的「減少動畫」章節,以取得 SwiftUI / Compose 等效寫法。
效能檢查清單
- 僅對
transform和opacity做動畫(僅合成屬性) - 謹慎使用
will-change,並在動畫完成後移除 - JS 動畫優先使用
requestAnimationFrame而非setTimeout/setInterval - 滾動驅動:CSS
animation-timeline> JSIntersectionObserver> 滾動監聽器 - 在低階裝置上測試(在 DevTools 中將 CPU 降速 4 倍)
快速參考:載入子技能
| 需求 | 載入 |
|---|---|
| 緩動深入探討、spring 配置 | references/easing-guide.md |
| 可複製貼上的進出場模式 | references/enter-exit-recipes.md |
| 設計師偏好的風格選擇 | references/designers.md |
| 行動 UX 情境 | ../mobile-principles/SKILL.md |
| 桌面 UX 情境 | ../desktop-principles/SKILL.md |
| GSAP 特定 | ../gsap/SKILL.md |
| Framer Motion 特定 | ../framer-motion/SKILL.md |
| 純 CSS 動畫 | ../css-native/SKILL.md |
| Three.js / R3F | ../threejs-r3f/SKILL.md |
| Canvas / 生成式 | ../canvas-generative/SKILL.md |
| Compose Android 動畫 | ../compose-motion/SKILL.md |
| Compose Multiplatform | ../compose-multiplatform/SKILL.md |
| SwiftUI iOS/macOS 動畫 | ../swiftui-motion/SKILL.md |
| Compose 進階圖形 | ../compose-graphics/SKILL.md |
| SwiftUI 進階圖形 | ../swiftui-graphics/SKILL.md |
| 視覺 / 動態 / 無障礙審查 | ../design-audit/SKILL.md |
| UI/UX 智慧(84 種風格、192 種調色盤) | ../ui-ux-pro-max/SKILL.md |