GSAP
核心補間方法
- gsap.to(targets, vars) — 從目前狀態移動到
vars。最常用。 - gsap.from(targets, vars) — 從
vars移動到目前狀態(進場效果)。 - gsap.fromTo(targets, fromVars, toVars) — 明確指定起始與結束。
- gsap.set(targets, vars) — 立即套用(持續時間 0)。
務必使用 駝峰式 (camelCase) 屬性名稱(例如 backgroundColor、rotationX)。
常用變數 (vars)
- duration — 秒數(預設 0.5)。
- delay — 開始前的延遲秒數。
- ease —
"power1.out"(預設)、"power3.inOut"、"back.out(1.7)"、"elastic.out(1, 0.3)"、"none"。 - stagger — 數字
0.1或物件:{ amount: 0.3, from: "center" }、{ each: 0.1, from: "random" }。 - overwrite —
false(預設)、true或"auto"。 - repeat — 數字或
-1表示無限。yoyo — 與 repeat 搭配,來回交替方向。 - onComplete、onStart、onUpdate — 回呼函數。
- immediateRender — 對 from()/fromTo() 預設為
true。若後續補間要針對相同屬性+元素,請設為false以避免被覆寫。
Transform 與 CSS
優先使用 GSAP 的 transform 別名 而非原始 transform 字串:
| GSAP 屬性 | 等效 CSS |
|---|---|
x、y、z | translateX/Y/Z (px) |
xPercent、yPercent | translateX/Y (%) |
scale、scaleX、scaleY | scale |
rotation | rotate (deg) |
rotationX、rotationY | 3D rotate |
skewX、skewY | skew |
transformOrigin | transform-origin |
- autoAlpha — 優先於
opacity。值為 0 時,同時將visibility設為hidden。 - CSS 變數 —
"--hue": 180。 - svgOrigin (僅 SVG) — 全域 SVG 座標空間原點。請勿與
transformOrigin合併使用。 - 方向性旋轉 —
"360_cw"、"-170_short"、"90_ccw"。 - clearProps —
"all"或逗號分隔;在完成時移除行內樣式。 - 相對值 —
"+=20"、"-=10"、"*=2"。
函數式值
gsap.to(".item", {
x: (i, target, targets) => i * 50,
stagger: 0.1,
});
漸變函數 (Easing)
內建緩動:power1–power4、back、bounce、circ、elastic、expo、sine。每個都有 .in、.out、.inOut。
預設值
gsap.defaults({ duration: 0.6, ease: "power2.out" });
控制補間
const tween = gsap.to(".box", { x: 100 });
tween.pause();
tween.play();
tween.reverse();
tween.kill();
tween.progress(0.5);
tween.time(0.2);
gsap.matchMedia()(響應式 + 無障礙)
僅在媒體查詢符合時執行設定;不符合時自動復原。
let mm = gsap.matchMedia();
mm.add(
{
isDesktop: "(min-width: 800px)",
reduceMotion: "(prefers-reduced-motion: reduce)",
},
(context) => {
const { isDesktop, reduceMotion } = context.conditions;
gsap.to(".box", {
rotation: isDesktop ? 360 : 180,
duration: reduceMotion ? 0 : 2,
});
},
);
時間軸 (Timelines)
建立時間軸
const tl = gsap.timeline({ defaults: { duration: 0.5, ease: "power2.out" } });
tl.to(".a", { x: 100 }).to(".b", { y: 50 }).to(".c", { opacity: 0 });
位置參數
第三個參數控制放置位置:
- 絕對值:
1— 在第 1 秒 - 相對值:
"+=0.5"— 在結尾之後;"-=0.2"— 在結尾之前 - 標籤:
"intro"、"intro+=0.3" - 對齊:
"<"— 與前一個補間同時開始;">"— 在前一個補間結束後開始;"<0.2"— 在前一個補間開始後 0.2 秒
tl.to(".a", { x: 100 }, 0);
tl.to(".b", { y: 50 }, "<"); // 與 .a 同時開始
tl.to(".c", { opacity: 0 }, "<0.2"); // 在 .b 開始後 0.2 秒
標籤
tl.addLabel("intro", 0);
tl.to(".a", { x: 100 }, "intro");
tl.addLabel("outro", "+=0.5");
tl.play("outro");
tl.tweenFromTo("intro", "outro");
時間軸選項
- paused: true — 建立時暫停;需呼叫
.play()開始。 - repeat、yoyo — 套用至整個時間軸。
- defaults — 變數會合併到每個子補間中。
巢狀時間軸
const master = gsap.timeline();
const child = gsap.timeline();
child.to(".a", { x: 100 }).to(".b", { y: 50 });
master.add(child, 0);
播放控制
tl.play()、tl.pause()、tl.reverse()、tl.restart()、tl.time(2)、tl.progress(0.5)、tl.kill()。
效能
優先使用 Transform 與 Opacity
對 x、y、scale、rotation、opacity 做動畫會停留在合成器層。若可使用 transform 達到相同效果,請避免使用 width、height、top、left。
will-change
will-change: transform;
僅在確實有動畫的元素上使用。
使用 gsap.quickTo() 處理頻繁更新
let xTo = gsap.quickTo("#id", "x", { duration: 0.4, ease: "power3" }),
yTo = gsap.quickTo("#id", "y", { duration: 0.4, ease: "power3" });
container.addEventListener("mousemove", (e) => {
xTo(e.pageX);
yTo(e.pageY);
});
使用 Stagger 取代大量補間
使用 stagger 而非分散的多個補間加上手動延遲。
清理
暫停或結束(kill)不在畫面中的動畫。
參考資料(按需載入)
- references/effects.md — 即用特效:打字機文字、音訊可視化器。在需要為 HyperFrames 取得現成特效模式時閱讀。
最佳實務
- 使用駝峰式屬性名稱;優先使用 transform 別名與 autoAlpha。
- 優先使用時間軸而非以 delay 串連;使用位置參數。
- 使用
addLabel()加入標籤以利可讀的序列編排。 - 將預設值傳入時間軸建構函式。
- 需要控制播放時,儲存補間/時間軸的回傳值。
禁止事項
- 當 transform 足以替代時,不要對版面屬性(width/height/top/left)做動畫。
- 不要在相同 SVG 元素上同時使用 svgOrigin 與 transformOrigin。
- 當時間軸可以序列化時,不要用 delay 來串連動畫。
- 不要在 DOM 尚未存在時建立補間。
- 不要跳過清理動作 — 不再使用時務必清除(kill)補間。