gsap
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGSAP
GSAP
Core Tween Methods
核心补间方法
- gsap.to(targets, vars) — animate from current state to . Most common.
vars - gsap.from(targets, vars) — animate from to current state (entrances).
vars - gsap.fromTo(targets, fromVars, toVars) — explicit start and end.
- gsap.set(targets, vars) — apply immediately (duration 0).
Always use camelCase property names (e.g. , ).
backgroundColorrotationX- gsap.to(targets, vars) — 从当前状态动画过渡到定义的状态。最常用的方法。
vars - gsap.from(targets, vars) — 从定义的状态动画过渡到当前状态(常用于入场效果)。
vars - gsap.fromTo(targets, fromVars, toVars) — 明确指定起始和结束状态的动画。
- gsap.set(targets, vars) — 立即应用属性(时长为0)。
请始终使用camelCase命名属性(例如、)。
backgroundColorrotationXCommon vars
常用参数
- duration — seconds (default 0.5).
- delay — seconds before start.
- ease — (default),
"power1.out","power3.inOut","back.out(1.7)","elastic.out(1, 0.3)"."none" - stagger — number or object:
0.1,{ amount: 0.3, from: "center" }.{ each: 0.1, from: "random" } - overwrite — (default),
false, ortrue."auto" - repeat — number or for infinite. yoyo — alternates direction with repeat.
-1 - onComplete, onStart, onUpdate — callbacks.
- immediateRender — default for from()/fromTo(). Set
trueon later tweens targeting the same property+element to avoid overwrite.false
- 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 — 重复次数,设置为表示无限重复。yoyo — 配合repeat使用,使动画交替反向播放。
-1 - onComplete, onStart, onUpdate — 回调函数。
- immediateRender — from()/fromTo()方法的默认值为。针对同一元素的同一属性后续创建补间时,设置为
true可避免覆盖问题。false
Transforms and CSS
变换与CSS
Prefer GSAP's transform aliases over raw string:
transform| GSAP property | Equivalent |
|---|---|
| translateX/Y/Z (px) |
| translateX/Y in % |
| scale |
| rotate (deg) |
| 3D rotate |
| skew |
| transform-origin |
- autoAlpha — prefer over . At 0: also sets
opacity.visibility: hidden - CSS variables — .
"--hue": 180 - svgOrigin (SVG only) — global SVG coordinate space origin. Don't combine with .
transformOrigin - Directional rotation — ,
"360_cw","-170_short"."90_ccw" - clearProps — or comma-separated; removes inline styles on complete.
"all" - Relative values — ,
"+=20","-=10"."*=2"
优先使用GSAP的变换别名,而非原生字符串:
transform| GSAP属性 | 等效原生属性 |
|---|---|
| translateX/Y/Z(px) |
| translateX/Y(百分比) |
| scale |
| rotate(角度) |
| 3D旋转 |
| skew |
| transform-origin |
- autoAlpha — 优先于使用。当值为0时,会同时设置
opacity。visibility: hidden - CSS变量 — 例如。
"--hue": 180 - svgOrigin (仅SVG适用) — 全局SVG坐标系原点。请勿与同时使用。
transformOrigin - 定向旋转 — 例如、
"360_cw"、"-170_short"。"90_ccw" - clearProps — 设置为或逗号分隔的属性列表,动画结束时移除内联样式。
"all" - 相对值 — 例如、
"+=20"、"-=10"。"*=2"
Function-Based Values
函数式值
javascript
gsap.to(".item", {
x: (i, target, targets) => i * 50,
stagger: 0.1,
});javascript
gsap.to(".item", {
x: (i, target, targets) => i * 50,
stagger: 0.1,
});Easing
缓动效果
Built-in eases: –, , , , , , . Each has , , .
power1power4backbouncecircelasticexposine.in.out.inOut内置缓动函数包括:–、、、、、、。每个缓动函数都有、、变体。
power1power4backbouncecircelasticexposine.in.out.inOutDefaults
默认设置
javascript
gsap.defaults({ duration: 0.6, ease: "power2.out" });javascript
gsap.defaults({ duration: 0.6, ease: "power2.out" });Controlling Tweens
补间控制
javascript
const tween = gsap.to(".box", { x: 100 });
tween.pause();
tween.play();
tween.reverse();
tween.kill();
tween.progress(0.5);
tween.time(0.2);javascript
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() (Responsive + Accessibility)
gsap.matchMedia()(响应式 + 无障碍)
Runs setup only when a media query matches; auto-reverts when it stops matching.
javascript
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,
});
},
);仅当媒体查询匹配时执行设置,不匹配时自动恢复。
javascript
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
时间线
Creating a Timeline
创建时间线
javascript
const tl = gsap.timeline({ defaults: { duration: 0.5, ease: "power2.out" } });
tl.to(".a", { x: 100 }).to(".b", { y: 50 }).to(".c", { opacity: 0 });javascript
const tl = gsap.timeline({ defaults: { duration: 0.5, ease: "power2.out" } });
tl.to(".a", { x: 100 }).to(".b", { y: 50 }).to(".c", { opacity: 0 });Position Parameter
位置参数
Third argument controls placement:
- Absolute: — at 1s
1 - Relative: — after end;
"+=0.5"— before end"-=0.2" - Label: ,
"intro""intro+=0.3" - Alignment: — same start as previous;
"<"— after previous ends;">"— 0.2s after previous starts"<0.2"
javascript
tl.to(".a", { x: 100 }, 0);
tl.to(".b", { y: 50 }, "<"); // same start as .a
tl.to(".c", { opacity: 0 }, "<0.2"); // 0.2s after .b starts第三个参数控制动画在时间线中的位置:
- 绝对位置: — 位于时间线的1秒处
1 - 相对位置: — 在上一个动画结束后0.5秒开始;
"+=0.5"— 在上一个动画结束前0.2秒开始"-=0.2" - 标签: 、
"intro""intro+=0.3" - 对齐方式: — 与上一个动画同时开始;
"<"— 在上一个动画结束后开始;">"— 在上一个动画开始后0.2秒开始"<0.2"
javascript
tl.to(".a", { x: 100 }, 0);
tl.to(".b", { y: 50 }, "<"); // 与.a同时开始
tl.to(".c", { opacity: 0 }, "<0.2"); // 在.b开始后0.2秒开始Labels
标签
javascript
tl.addLabel("intro", 0);
tl.to(".a", { x: 100 }, "intro");
tl.addLabel("outro", "+=0.5");
tl.play("outro");
tl.tweenFromTo("intro", "outro");javascript
tl.addLabel("intro", 0);
tl.to(".a", { x: 100 }, "intro");
tl.addLabel("outro", "+=0.5");
tl.play("outro");
tl.tweenFromTo("intro", "outro");Timeline Options
时间线选项
- paused: true — create paused; call to start.
.play() - repeat, yoyo — apply to whole timeline.
- defaults — vars merged into every child tween.
- paused: true — 创建时处于暂停状态;调用启动。
.play() - repeat, yoyo — 应用于整个时间线。
- defaults — 合并到每个子补间的参数。
Nesting Timelines
嵌套时间线
javascript
const master = gsap.timeline();
const child = gsap.timeline();
child.to(".a", { x: 100 }).to(".b", { y: 50 });
master.add(child, 0);javascript
const master = gsap.timeline();
const child = gsap.timeline();
child.to(".a", { x: 100 }).to(".b", { y: 50 });
master.add(child, 0);Playback Control
播放控制
tl.play()tl.pause()tl.reverse()tl.restart()tl.time(2)tl.progress(0.5)tl.kill()tl.play()tl.pause()tl.reverse()tl.restart()tl.time(2)tl.progress(0.5)tl.kill()Performance
性能优化
Prefer Transform and Opacity
优先使用变换与透明度
Animating , , , , stays on the compositor. Avoid , , , when transforms achieve the same effect.
xyscalerotationopacitywidthheighttopleft动画、、、、会在合成器层处理,性能更优。当变换属性可实现相同效果时,避免动画、、、等布局属性。
xyscalerotationopacitywidthheighttopleftwill-change
will-change
css
will-change: transform;Only on elements that actually animate.
css
will-change: transform;仅在实际需要动画的元素上使用。
gsap.quickTo() for Frequent Updates
高频更新使用gsap.quickTo()
javascript
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);
});javascript
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 > Many Tweens
优先使用stagger而非多个补间
Use instead of separate tweens with manual delays.
stagger使用替代手动添加延迟的多个独立补间。
staggerCleanup
清理
Pause or kill off-screen animations.
暂停或销毁屏幕外的动画。
References (loaded on demand)
参考文档(按需加载)
- references/effects.md — Drop-in effects: typewriter text, audio visualizer. Read when needing ready-made effect patterns for HyperFrames.
- references/effects.md — 即插即用效果:打字机文本、音频可视化。当需要为HyperFrames使用现成效果模式时阅读。
Best Practices
最佳实践
- Use camelCase property names; prefer transform aliases and autoAlpha.
- Prefer timelines over chaining with delay; use the position parameter.
- Add labels with for readable sequencing.
addLabel() - Pass defaults into timeline constructor.
- Store tween/timeline return value when controlling playback.
- 使用camelCase命名属性;优先使用变换别名和autoAlpha。
- 优先使用时间线而非通过delay链式调用;使用位置参数。
- 使用添加标签,提升序列可读性。
addLabel() - 将默认参数传入时间线构造函数。
- 保存补间/时间线的返回值以便控制播放。
Do Not
注意事项
- Animate layout properties (width/height/top/left) when transforms suffice.
- Use both svgOrigin and transformOrigin on the same SVG element.
- Chain animations with delay when a timeline can sequence them.
- Create tweens before the DOM exists.
- Skip cleanup — always kill tweens when no longer needed.
- 当变换属性可实现相同效果时,请勿动画布局属性(width/height/top/left)。
- 请勿在同一SVG元素上同时使用svgOrigin和transformOrigin。
- 当时间线可实现序列编排时,请勿通过delay链式调用动画。
- 请勿在DOM未加载前创建补间。
- 务必清理动画——不再需要时始终销毁补间。