gsap-frameworks by greensock/gsap-skills
npx skills add https://github.com/greensock/gsap-skills --skill gsap-frameworks当在 Vue(或 Nuxt)、Svelte(或 SvelteKit)或其他使用生命周期(mounted/unmounted)的组件框架中编写或审查 GSAP 代码时应用。对于 React,请专门使用 gsap-react(useGSAP hook, gsap.context())。
相关技能: 对于补间动画和时间轴,使用 gsap-core 和 gsap-timeline;对于基于滚动的动画,使用 gsap-scrolltrigger;对于 React,使用 gsap-react。
.box 及类似选择器只会匹配该组件内部的元素,而不是页面的其余部分。使用 onMounted 在组件挂载到 DOM 后运行 GSAP。使用 onUnmounted 进行清理。
import { onMounted, onUnmounted, ref } from "vue";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger); // 每个应用注册一次,例如在 main.js 中
export default {
setup() {
const container = ref(null);
let ctx;
onMounted(() => {
if (!container.value) return;
ctx = gsap.context(() => {
gsap.to(".box", { x: 100, duration: 0.6 });
gsap.from(".item", { autoAlpha: 0, y: 20, stagger: 0.1 });
}, container.value);
});
onUnmounted(() => {
ctx?.revert();
});
return { container };
}
};
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
container.value)作为第二个参数传入,这样像 .item 这样的选择器就会被限定在该根元素内。在回调函数内部创建的所有动画和 ScrollTrigger 都会被跟踪,并在调用 ctx.revert() 时被恢复。使用 <script setup> 和 refs 的思路相同:
<script setup>
import { onMounted, onUnmounted, ref } from "vue";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
const container = ref(null);
let ctx;
onMounted(() => {
if (!container.value) return;
ctx = gsap.context(() => {
gsap.to(".box", { x: 100 });
gsap.from(".item", { autoAlpha: 0, stagger: 0.1 });
}, container.value);
});
onUnmounted(() => {
ctx?.revert();
});
</script>
<template>
<div ref="container">
<div class="box">Box</div>
<div class="item">Item</div>
</div>
</template>
使用 onMount 在 DOM 准备就绪后运行 GSAP。使用从 onMount 返回的清理函数(或在响应式块/组件销毁中跟踪上下文并进行清理)来恢复。Svelte 5 使用不同的生命周期;但原则相同:在 "mounted" 时创建,在 "destroyed" 时恢复。
<script>
import { onMount } from "svelte";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
let container;
onMount(() => {
if (!container) return;
const ctx = gsap.context(() => {
gsap.to(".box", { x: 100 });
gsap.from(".item", { autoAlpha: 0, stagger: 0.1 });
}, container);
return () => ctx.revert();
});
</script>
<div bind:this={container}>
<div class="box">Box</div>
<div class="item">Item</div>
</div>
不要使用可能匹配当前组件外部元素的全局选择器。始终将作用域(容器元素或引用)作为第二个参数传递给 gsap.context(callback, scope),以便在回调内运行的任何选择器都仅限于该子树。
.box 仅在 containerRef 内部搜索。当您在补间动画/时间轴上使用 scrollTrigger 配置或使用 ScrollTrigger.create() 时,会创建 ScrollTrigger 实例。它们包含在 gsap.context() 中,并在您调用 ctx.revert() 时被恢复。因此:
| 生命周期 | 操作 |
|---|---|
| Mounted | 在 gsap.context(scope) 内部创建补间动画和 ScrollTrigger。 |
| Unmount / Destroy | 调用 ctx.revert(),以便终止该上下文中所有动画和 ScrollTrigger 并恢复内联样式。 |
不要在组件的 setup 中或在根元素存在之前运行的同步顶级脚本中创建 GSAP 动画。等待 onMounted / onMount(或等效时机),以便容器引用已存在于 DOM 中。
每周安装量
924
代码仓库
GitHub 星标数
1.6K
首次出现
5 天前
安全审计
安装于
codex916
opencode916
cursor916
kimi-cli914
gemini-cli914
github-copilot914
Apply when writing or reviewing GSAP code in Vue (or Nuxt), Svelte (or SvelteKit), or other component frameworks that use a lifecycle (mounted/unmounted). For React specifically, use gsap-react (useGSAP hook, gsap.context()).
Related skills: For tweens and timelines use gsap-core and gsap-timeline ; for scroll-based animation use gsap-scrolltrigger ; for React use gsap-react.
.box and similar only match elements inside that component, not the rest of the page.Use onMounted to run GSAP after the component is in the DOM. Use onUnmounted to clean up.
import { onMounted, onUnmounted, ref } from "vue";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger); // once per app, e.g. in main.js
export default {
setup() {
const container = ref(null);
let ctx;
onMounted(() => {
if (!container.value) return;
ctx = gsap.context(() => {
gsap.to(".box", { x: 100, duration: 0.6 });
gsap.from(".item", { autoAlpha: 0, y: 20, stagger: 0.1 });
}, container.value);
});
onUnmounted(() => {
ctx?.revert();
});
return { container };
}
};
container.value) as the second argument so selectors like .item are scoped to that root. All animations and ScrollTriggers created inside the callback are tracked and reverted when ctx.revert() is called.Same idea with <script setup> and refs:
<script setup>
import { onMounted, onUnmounted, ref } from "vue";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
const container = ref(null);
let ctx;
onMounted(() => {
if (!container.value) return;
ctx = gsap.context(() => {
gsap.to(".box", { x: 100 });
gsap.from(".item", { autoAlpha: 0, stagger: 0.1 });
}, container.value);
});
onUnmounted(() => {
ctx?.revert();
});
</script>
<template>
<div ref="container">
<div class="box">Box</div>
<div class="item">Item</div>
</div>
</template>
Use onMount to run GSAP after the DOM is ready. Use the returned cleanup function from onMount (or track the context and clean up in a reactive block / component destroy) to revert. Svelte 5 uses a different lifecycle; the same principle applies: create in “mounted” and revert in “destroyed.”
<script>
import { onMount } from "svelte";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
let container;
onMount(() => {
if (!container) return;
const ctx = gsap.context(() => {
gsap.to(".box", { x: 100 });
gsap.from(".item", { autoAlpha: 0, stagger: 0.1 });
}, container);
return () => ctx.revert();
});
</script>
<div bind:this={container}>
<div class="box">Box</div>
<div class="item">Item</div>
</div>
Do not use global selectors that can match elements outside the current component. Always pass the scope (container element or ref) as the second argument to gsap.context(callback, scope) so that any selector run inside the callback is limited to that subtree.
.box is only searched inside containerRef.ScrollTrigger instances are created when you use the scrollTrigger config on a tween/timeline or ScrollTrigger.create(). They are included in gsap.context() and reverted when you call ctx.revert(). So:
| Lifecycle | Action |
|---|---|
| Mounted | Create tweens and ScrollTriggers inside gsap.context(scope). |
| Unmount / Destroy | Call ctx.revert() so all animations and ScrollTriggers in that context are killed and inline styles reverted. |
Do not create GSAP animations in the component’s setup or in a synchronous top-level script that runs before the root element exists. Wait for onMounted / onMount (or equivalent) so the container ref is in the DOM.
Weekly Installs
924
Repository
GitHub Stars
1.6K
First Seen
5 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex916
opencode916
cursor916
kimi-cli914
gemini-cli914
github-copilot914
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装