前端开发
在 Web/React/Vue/Svelte 中实现…
需要在网页或前端框架中渲染设计师产出的 After Effects 动画、加载动画、图标微交互,并支持播放、暂停、跳转、滚动、悬停控制时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
在 Web/React/Vue/Svel特定
解决思路
- 选择 Lottie 格式:生产环境默认用 .lottie(dotLottie),因为它是 ZIP 压缩包,包含 JSON + 资源,支持一个文件内多个动画和主题,体积最多减少 90%;需要人类可读、广泛兼容或调试时用 .json。
- 用 After Effects + Bodymovin 插件导出 JSON Lottie;导出设置直接影响最终文件大小。
- 使用 lottie-web:import lottie from 'lottie-web';调用 lottie.loadAnimation({ container: document.getElementById('lottie-container'), renderer: 'svg', loop: true, autoplay: true, path: 'animation.json' });本地数据时传 animationData: jsonData。
- 使用 @lottiefiles/dotlottie-web(推荐):import { DotLottie } from '@lottiefiles/dotlottie-web';new DotLottie({ canvas: document.getElementById('canvas'), src: 'animation.lottie', autoplay: true, loop: true })。
- 在 React 中用 @lottiefiles/dotlottie-react:import { DotLottieReact } from '@lottiefiles/dotlottie-react';渲染 <DotLottieReact src='animation.lottie' loop autoplay style={{ height: 300 }} />。
- 或在 React 中用 lottie-react 作为替代:import Lottie from 'lottie-react';import animationData from './animation.json';渲染 <Lottie animationData={animationData} loop={true} />。
- 从 LottieFiles/lottie.host 获取现成动画:使用 100,000+ 免费动画、直接 URL 嵌入和 CDN 托管。
- 使用本地 JSON/dotLottie 文件:打包进应用以减少网络请求,并便于版本控制。
- 使用 After Effects 导出自定义动画:安装 Bodymovin 插件,重点检查导出设置以控制文件大小。
- 基础 HTML 集成 dotLottie-web:创建 <canvas id='canvas'>,CSS 设置 width/height,在 <script type='module'> 中 import { DotLottie } from 'https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web/+esm',然后 new DotLottie({ canvas: document.getElementById('canvas'), src: 'https://lottie.host/4db68bbd-31f6-4cd8-84eb-189de081159a/IGmMCqhzpt.lottie', autoplay: true, loop: true })。
- React 播放控制:用 useState 保存 dotLottie 实例;handlePlay = () => dotLottie?.play();handlePause = () => dotLottie?.pause();handleStop = () => dotLottie?.stop();handleSeek = (frame) => dotLottie?.setFrame(frame);DotLottieReact 设置 dotLottieRefCallback={setDotLottie}、autoplay={false}、loop,并添加 Play/Pause/Stop/Seek 按钮。
- 事件监听与生命周期:在 useEffect 中判断 dotLottie 存在;定义 onLoad/onPlay/onPause/onComplete/onFrame;用 dotLottie.addEventListener('load'|'play'|'pause'|'complete'|'frame', handler);return cleanup 中使用 removeEventListener 移除相同 handler。
- 用 lottie-react 做滚动驱动动画:配置 interactivity = { mode: 'scroll', actions: [ { visibility: [0, 0.2], type: 'stop', frames: [0] }, { visibility: [0.2, 0.45], type: 'seek', frames: [0, 45] }, { visibility: [0.45, 1.0], type: 'loop', frames: [45, 60] } ] };传给 <Lottie animationData={robotAnimation} interactivity={interactivity} />。
- 悬停触发片段播放:使用 useLottie({ animationData: likeButton }) 和 useLottieInteractivity({ lottieObj, mode: 'cursor', actions: [ { position: { x: [0, 1], y: [0, 1] }, type: 'loop', frames: [45, 60] }, { position: { x: -1, y: -1 }, type: 'stop', frames: [45] } ] })。
- 支持多动画和主题:用 DotLottieReact 和 dotLottieRefCallback;load 事件中读取 dotLottie.manifest.animations 和 dotLottie.manifest.themes,保存 activeAnimationId/activeThemeId;通过 animationId、themeId props 切换,并用 select 下拉绑定。
- 用 Web Worker 提升性能:import { DotLottieWorker } from '@lottiefiles/dotlottie-web';new DotLottieWorker({ canvas: document.getElementById('canvas'), src: 'heavy-animation.lottie', autoplay: true, loop: true, workerId: 'worker-1' });多个动画用不同 workerId 分组或新增 DotLottieWorker。
- 与 GSAP ScrollTrigger 集成:注册 gsap.registerPlugin(ScrollTrigger);用 useRef 获取 lottie 实例;useEffect 中 gsap.to(anim, { scrollTrigger: { trigger: '#animation-section', start: 'top center', end: 'bottom center', scrub: 1, onUpdate: (self) => { const frame = Math.floor(self.progress * (anim.totalFrames - 1)); anim.goToAndStop(frame, true); } } });外层 div 高度 200vh;Lottie 设置 autoplay={false} loop={false}。
- 与 Framer Motion 集成:用 motion.div 包裹 DotLottieReact,设置 initial={{ opacity: 0, scale: 0.8 }}、animate={{ opacity: 1, scale: 1 }}、transition={{ duration: 0.6 }}。
- Vue 3 集成:import { DotLottieVue } from '@lottiefiles/dotlottie-vue';在 template 中使用 <DotLottieVue style='height: 500px; width: 500px' autoplay loop src='https://path-to-animation.lottie' />。
- Svelte 集成:import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';import type { DotLottie };let dotLottie: DotLottie | null = null;function play() { dotLottie?.play(); };使用 <DotLottieSvelte src='animation.lottie' loop={true} autoplay={true} dotLottieRefCallback={(ref) => dotLottie = ref} /> 和按钮 on:click={play}。
- 文件大小优化—After Effects 导出设置:启用 'Skip images that aren't used'。
- 文件大小优化—After Effects 导出设置:尽可能用 'Glyphs' 替代字体。
- 文件大小优化—After Effects 导出设置:简化路径,在 Illustrator 中减少点。
- 文件大小优化—After Effects 导出设置:避免会生成大量数据的粒子、噪声等效果。
- 文件大小优化—After Effects 导出设置:用形状图层而不是矢量图层。
- 文件大小优化—压缩:生产用 dotLottie (.lottie) 格式自动压缩。
- 文件大小优化—压缩:把 JSON 跑 Lottie optimizer 工具。
- 文件大小优化—压缩:删除不必要的 metadata。
- 文件大小优化—懒加载:用 IntersectionObserver 监听 trigger 元素;进入视口时 setShouldLoad(true);在渲染中 shouldLoad && <DotLottieReact src='animation.lottie' loop autoplay />;cleanup 调用 observer.disconnect()。
- 运行时性能—渲染器选择:SVG 质量最好,但复杂动画较慢。
- 运行时性能—渲染器选择:Canvas 性能更好,但会栅格化。
- 运行时性能—渲染器选择:HTML 支持有限,仅适合简单动画。
- 运行时性能—渲染器选择:复杂动画优先 Canvas,并用 renderConfig: { devicePixelRatio: window.devicePixelRatio || 1 }。
- 运行时性能—Web Workers:用 DotLottieWorker 把重动画卸载到 worker。
- 运行时性能—移动端:用 /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) 判断移动端。
- 运行时性能—移动端:移动端加载 animation-low.lottie,桌面加载 animation-high.lottie。
- 运行时性能—移动端:renderConfig.devicePixelRatio 移动端设为 1,否则设为 window.devicePixelRatio。
- 避免内存泄漏:在 useEffect 的 cleanup 中调用 dotLottie?.destroy()。
- 避免事件监听泄漏:添加监听后,cleanup 中必须 removeEventListener 同一个 handler。
- 当导出 JSON 超过 500KB 时,简化 After Effects 合成,减少图层和关键帧。
- 当导出 JSON 超过 500KB 时,使用 dotLottie 格式压缩。
- 当导出 JSON 超过 500KB 时,检查 Bodymovin 导出设置,不需要时关闭 'Include expressions'。
- 当导出 JSON 超过 500KB 时,导出前移除未使用资源。
- 当导出 JSON 超过 500KB 时,用 https://lottiefiles.com/tools/lottie-editor 优化。
- 解决动画卡顿:从 SVG 切换到 Canvas 渲染器。
- 解决动画卡顿:用 DotLottieWorker 做 web worker 渲染。
- 解决动画卡顿:在 After Effects 中减少图层、简化形状。
- 解决动画卡顿:移动端降低 devicePixelRatio。
- 解决动画卡顿:避免同时动画太多属性。
- 解决路径/URL 问题:本地打包优先 import animationData from './animation.json' 并传 animationData。
- 解决路径/URL 问题:外部 URL 用 src,并确保服务端有 CORS 头。
- 解决路径/URL 问题:Next.js 放到 public/ 目录,并用 src='/animations/animation.lottie'。
- 处理 After Effects 导出兼容性:注意 Lottie 不支持图层效果(drop shadows、glows),改用形状图层。
- 处理 After Effects 导出兼容性:混合模式仅有限支持。
- 处理 After Effects 导出兼容性:3D 图层不支持。
- 处理 After Effects 导出兼容性:表达式仅部分支持。
- 处理 After Effects 导出兼容性:轨道遮罩仅部分支持。
- 处理 After Effects 导出兼容性:尽早频繁测试导出。
- 处理 After Effects 导出兼容性:导出前用 LottieFiles preview 预览。
- 处理 After Effects 导出兼容性:检查 Bodymovin 兼容性列表 https://airbnb.io/lottie/#/supported-features。
- 处理 After Effects 导出兼容性:尽可能把效果转换为形状。
- 使用 scripts/generate_lottie_component.py 生成 React/Vue/Svelte Lottie 组件样板。
- 使用 scripts/optimize_lottie.py 优化 Lottie JSON 文件大小。
- 查阅 references/api_reference.md 获取 lottie-web、lottie-react、dotlottie-web 完整 API。
- 查阅 references/after_effects_export.md 获取 After Effects 导出指南。
- 查阅 references/performance_guide.md 获取详细性能优化策略。
- 使用 assets/starter_lottie/ 作为完整 React + Vite + Lottie starter template。
- 参考 assets/examples/ 中的真实 Lottie 动画模式和用例。
- 与 gsap-scrolltrigger 技能配合做滚动驱动 Lottie 动画同步页面滚动。
- 与 motion-framer 技能配合,用 Framer Motion 包裹 Lottie 做布局动画。
- 与 animated-component-libraries 技能配合,使用可能内置 Lottie 动画的预构建组件。
- 与 threejs-webgl 技能配合处理超出 Lottie 2D 能力的 3D 动画。
- 与 react-three-fiber 技能配合处理复杂 3D 动画场景替代方案。
适用边界 · 注意事项
- 需要真正的 3D 动画、复杂 WebGL 场景或超出 Lottie 2D 能力的效果时,不要用 Lottie,改用 threejs-webgl/react-three-fiber。
- After Effects 中使用了 Lottie/Bodymovin 不支持的特性(图层效果、3D 图层、部分表达式、轨道遮罩、混合模式)且无法转换为形状时,不要强行导出。
- 只需简单 CSS/JS 动画、没有设计师产出的 After Effects 动画或不想引入 Lottie 运行时时,不值得使用。