五种缓动对比
同一段位移,换不同 ease 手感完全不同。点击重播,观察谁先冲出、谁带回弹。
gsap.to(el, { x: 180, duration: 0.9, ease: "back.out(1.7)" }); // 各自不同 ease
每个技能讲清三件事:它是什么、何时用、怎么用。vanilla 类技能全部配了可交互现场样例,React / Vue 类给出官方推荐写法。
gsap-core核心 API 与基础补间GSAP 引擎的核心技能:gsap.to() / from() / fromTo() 三种补间方法、缓动、时长、stagger、默认值,以及面向响应式与无障碍的 gsap.matchMedia()。这是所有其他技能的地基。
gsap.to(".box", { x: 100, rotation: 360,
duration: 0.8, ease: "power2.inOut" });
gsap.from(el, { autoAlpha: 0, y: 20, stagger: 0.1 });
gsap.fromTo(el, { x: 0 }, { x: 200, duration: 1 });
同一段位移,换不同 ease 手感完全不同。点击重播,观察谁先冲出、谁带回弹。
gsap.to(el, { x: 180, duration: 0.9, ease: "back.out(1.7)" }); // 各自不同 ease
vars 里传函数,GSAP 会为每个目标单独求值一次——按序号递增位移就是这么写的。
gsap.to(items, { x: (i) => i * 26, // 每个目标单独求值 y: (i) => i % 2 ? -14 : 14, stagger: 0.06, duration: 0.5 });
gsap-timeline序列编排与播放控制时间线把多个补间按顺序(或指定时刻)编排成一条可整体控制的动画线,替代"链式 delay"的官方做法。支持 position 参数、标签、嵌套与暂停/播放/反转。
const tl = gsap.timeline({ defaults: { duration: 0.5, ease: "power2.out" } });
tl.to(a, { x: 100 }) // 依序追加
.to(b, { y: 50 }, "<0.2") // 在上一段开始后 0.2s
.to(c, { opacity: 0 }, "-=0.1"); // 提前 0.1s 收尾
tl.pause(); tl.play(); tl.reverse(); tl.restart();
三步时间线(平移 → 变色 → 缩放)。试试播放、暂停、倒退、重播四条控制。
const tl = gsap.timeline({ defaults: { duration: 0.5 } }); tl.to(box, { x: 170, duration: 0.7 }) .to(box, { backgroundColor: "#7CF9AB" }) .to(box, { scale: 0.55 }, "+=0.1"); tl.pause(); tl.play(); tl.reverse(); tl.restart();
A 先走;B 用 <0.2 在 A 开始 0.2s 后并行;C 用默认 > 等 A 结束再动。
const tl = gsap.timeline(); tl.to(a, { x: 150, duration: 0.8 }) // A 起点 .to(b, { x: 150 }, "<0.2") // A 开始后 0.2s .to(c, { x: 150 }); // 默认 ">":A 结束后
gsap-scrolltrigger滚动联动、固定与跟手把动画与滚动位置绑定的技能:元素进入视口触发、pin 固定区块、scrub 让进度跟手、batch 批量入场、toggleActions 控制进出行为。
gsap.to(el, { x: 300,
scrollTrigger: {
trigger: el, start: "top center",
end: "bottom center",
toggleActions: "play reverse play reverse",
scrub: 1 // 或 true:进度跟手
}
});
继续向下滚动:舞台被固定在视口内,进度条随滚动填充,数字实时跟手。
ScrollTrigger.create({ trigger: stage, start: "top top", end: "+=500", pin: true, scrub: 1, onUpdate: (self) => { num.textContent = Math.round(self.progress * 100) + "%"; } });
方块进入视口时播放,离开时反向回退——常用于"进入才现身"的内容。
gsap.to(box, { x: 200, rotation: 180, scrollTrigger: { trigger: stage, start: "top 70%", toggleActions: "play none none reverse" } });
gsap-plugins布局翻转、拖拽与文本/SVG 特效GSAP 插件生态的总入口:Flip(布局翻转)、Draggable(拖拽)、ScrollTo / ScrollSmoother(滚动)、SplitText / ScrambleText(文本)、DrawSVG / MorphSVG / MotionPath(SVG)、CustomEase(自定义缓动)、Physics2D(物理)等。自 Webflow 收购起全部插件免费,无需会员与注册。
gsap.registerPlugin(Flip, Draggable);
const state = Flip.getState(".item");
// 改变 DOM(重排 / 增删 / 换 class)
Flip.from(state, { duration: 0.5, ease: "power2.inOut" });
Draggable.create(".knob", { type: "x", bounds: "#stage", inertia: true });
记录状态 → 打乱 DOM → Flip 平滑补间。还原同理,全程无手工坐标。
const state = Flip.getState(items); // 改 DOM:打乱顺序 shuffle(items) // items.forEach(...) 重新 append Flip.from(state, { duration: 0.5, ease: "power2.inOut" });
方块可在舞台内任意拖动,碰到边界有回弹阻力(edgeResistance)。
Draggable.create(box, { type: "x,y", bounds: stage, // 限制在舞台内 edgeResistance: 0.7 // 边界回弹阻力 });
gsap-utils数学与数组工具函数一组纯函数:clamp(夹取)、mapRange(区间映射)、normalize / interpolate(归一化 / 插值)、random / snap / shuffle(随机 / 吸附 / 打乱)、wrap(循环取值)、toArray / selector / pipe(集合处理)。无需注册,直接 gsap.utils.xxx()。
gsap.utils.clamp(0, 100, 150); // 100 gsap.utils.mapRange(0, 100, 0, 260, 50); // 130 gsap.utils.interpolate("#0AE448", "#7CF9AB", 0.5); gsap.utils.random(-100, 100); // 随机数 const map = gsap.utils.mapRange(0, 1, 0, 360); // 省略取值 = 返回函数
拖动滑块:mapRange 把 0–100 映射成位移,interpolate 在两种颜色间插值。
const x = gsap.utils.mapRange(0, 100, 0, 260, v); const color = gsap.utils.interpolate("#0AE448", "#7CF9AB", v / 100); gsap.set(box, { x, backgroundColor: color });
每次点击,六个方块各自随机位移、旋转与颜色——全部由 gsap.utils.random 生成。
gsap.to(items, { x: gsap.utils.random(-60, 60), rotation: gsap.utils.random(-180, 180), backgroundColor: gsap.utils.random(["#0AE448", "#7CF9AB", "#3A7D50"]), duration: 0.6, stagger: 0.05, overwrite: true });
gsap-reactuseGSAP 钩子与组件生命周期React / Next.js 中正确使用 GSAP 的官方模式:优先 @gsap/react 的 useGSAP 钩子,配合 scope 限定选择器、contextSafe 包裹回调,卸载时自动 revert 全部动画与 ScrollTrigger。
这是官方推荐写法(需在 React 工程中运行,纯静态页无法现场执行)。scope 保证选择器只匹配组件内部。
import { useGSAP } from "@gsap/react";
gsap.registerPlugin(useGSAP); // 应用入口注册一次
const containerRef = useRef(null);
useGSAP(() => {
gsap.to(".box", { x: 100 });
gsap.from(".item", { autoAlpha: 0, y: 20, stagger: 0.1 });
}, { scope: containerRef }); // 选择器只匹配组件内部
gsap-performance60fps 的动画写法让动画保持流畅的性能指南:只动 transform 与 opacity(交给合成器、跳过布局)、用 stagger 批量而非逐条 delay、高频更新用 gsap.quickTo() 复用单个 tween、适时清理离屏动画。
一个 tween + stagger 驱动 120 个方块,全部只动 transform 属性——即使量大也保持流畅。
gsap.to(".mini", { // 120 个目标,一条 tween x: "random(-14, 14)", rotation: "random(-120, 120)", duration: 1.2, ease: "power1.inOut", stagger: { each: 0.006 } }); // 只动 transform,不触发布局
高频 mousemove 下,quickTo 复用同一个 tween,只更新目标值——比每帧新建 tween 高效得多。
const xTo = gsap.quickTo(dot, "x", { duration: 0.4, ease: "power3" }); const yTo = gsap.quickTo(dot, "y", { duration: 0.4, ease: "power3" }); stage.addEventListener("mousemove", e => { xTo(e.offsetX - 10); yTo(e.offsetY - 10); // 每帧只改目标值 });
gsap-frameworksVue / Svelte / Nuxt 集成Vue、Svelte、Nuxt 等非 React 框架中集成 GSAP 的官方模式。三条铁律:DOM 就绪后创建(onMounted / onMount)、卸载时清理(ctx.revert())、选择器限定作用域(gsap.context(scope))。
官方推荐写法(需在对应框架工程中运行)。仓库内 examples/vue、examples/nuxt 有可运行工程。
// Vue 3(组合式 API)
import { onMounted, onUnmounted, ref } from "vue";
const container = ref(null);
let ctx;
onMounted(() => {
ctx = gsap.context(() => {
gsap.to(".box", { x: 100 });
gsap.from(".item", { autoAlpha: 0, y: 20, stagger: 0.1 });
}, container.value); // 作用域:只匹配组件内
});
onUnmounted(() => ctx?.revert()); // 卸载清理
// Svelte
onMount(() => {
const ctx = gsap.context(() => gsap.to(".box", { x: 100 }), container);
return () => ctx.revert(); // onMount 返回清理函数
});