呱呱聚合
← 返回基因库
前端开发

在 Web/React/Vue/Svelte 中实现…

需要在网页或前端框架中渲染设计师产出的 After Effects 动画、加载动画、图标微交互,并支持播放、暂停、跳转、滚动、悬停控制时使用。

被复用0 次
复用有效—
置信度55%
沉淀时间2026-09-23
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
来自 chloe.cao 的小呱沉淀
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特定

解决思路

  1. 选择 Lottie 格式:生产环境默认用 .lottie(dotLottie),因为它是 ZIP 压缩包,包含 JSON + 资源,支持一个文件内多个动画和主题,体积最多减少 90%;需要人类可读、广泛兼容或调试时用 .json。
  2. 用 After Effects + Bodymovin 插件导出 JSON Lottie;导出设置直接影响最终文件大小。
  3. 使用 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。
  4. 使用 @lottiefiles/dotlottie-web(推荐):import { DotLottie } from '@lottiefiles/dotlottie-web';new DotLottie({ canvas: document.getElementById('canvas'), src: 'animation.lottie', autoplay: true, loop: true })。
  5. 在 React 中用 @lottiefiles/dotlottie-react:import { DotLottieReact } from '@lottiefiles/dotlottie-react';渲染 <DotLottieReact src='animation.lottie' loop autoplay style={{ height: 300 }} />。
  6. 或在 React 中用 lottie-react 作为替代:import Lottie from 'lottie-react';import animationData from './animation.json';渲染 <Lottie animationData={animationData} loop={true} />。
  7. 从 LottieFiles/lottie.host 获取现成动画:使用 100,000+ 免费动画、直接 URL 嵌入和 CDN 托管。
  8. 使用本地 JSON/dotLottie 文件:打包进应用以减少网络请求,并便于版本控制。
  9. 使用 After Effects 导出自定义动画:安装 Bodymovin 插件,重点检查导出设置以控制文件大小。
  10. 基础 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 })。
  11. 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 按钮。
  12. 事件监听与生命周期:在 useEffect 中判断 dotLottie 存在;定义 onLoad/onPlay/onPause/onComplete/onFrame;用 dotLottie.addEventListener('load'|'play'|'pause'|'complete'|'frame', handler);return cleanup 中使用 removeEventListener 移除相同 handler。
  13. 用 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} />。
  14. 悬停触发片段播放:使用 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] } ] })。
  15. 支持多动画和主题:用 DotLottieReact 和 dotLottieRefCallback;load 事件中读取 dotLottie.manifest.animations 和 dotLottie.manifest.themes,保存 activeAnimationId/activeThemeId;通过 animationId、themeId props 切换,并用 select 下拉绑定。
  16. 用 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。
  17. 与 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}。
  18. 与 Framer Motion 集成:用 motion.div 包裹 DotLottieReact,设置 initial={{ opacity: 0, scale: 0.8 }}、animate={{ opacity: 1, scale: 1 }}、transition={{ duration: 0.6 }}。
  19. Vue 3 集成:import { DotLottieVue } from '@lottiefiles/dotlottie-vue';在 template 中使用 <DotLottieVue style='height: 500px; width: 500px' autoplay loop src='https://path-to-animation.lottie' />。
  20. 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}。
  21. 文件大小优化—After Effects 导出设置:启用 'Skip images that aren't used'。
  22. 文件大小优化—After Effects 导出设置:尽可能用 'Glyphs' 替代字体。
  23. 文件大小优化—After Effects 导出设置:简化路径,在 Illustrator 中减少点。
  24. 文件大小优化—After Effects 导出设置:避免会生成大量数据的粒子、噪声等效果。
  25. 文件大小优化—After Effects 导出设置:用形状图层而不是矢量图层。
  26. 文件大小优化—压缩:生产用 dotLottie (.lottie) 格式自动压缩。
  27. 文件大小优化—压缩:把 JSON 跑 Lottie optimizer 工具。
  28. 文件大小优化—压缩:删除不必要的 metadata。
  29. 文件大小优化—懒加载:用 IntersectionObserver 监听 trigger 元素;进入视口时 setShouldLoad(true);在渲染中 shouldLoad && <DotLottieReact src='animation.lottie' loop autoplay />;cleanup 调用 observer.disconnect()。
  30. 运行时性能—渲染器选择:SVG 质量最好,但复杂动画较慢。
  31. 运行时性能—渲染器选择:Canvas 性能更好,但会栅格化。
  32. 运行时性能—渲染器选择:HTML 支持有限,仅适合简单动画。
  33. 运行时性能—渲染器选择:复杂动画优先 Canvas,并用 renderConfig: { devicePixelRatio: window.devicePixelRatio || 1 }。
  34. 运行时性能—Web Workers:用 DotLottieWorker 把重动画卸载到 worker。
  35. 运行时性能—移动端:用 /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) 判断移动端。
  36. 运行时性能—移动端:移动端加载 animation-low.lottie,桌面加载 animation-high.lottie。
  37. 运行时性能—移动端:renderConfig.devicePixelRatio 移动端设为 1,否则设为 window.devicePixelRatio。
  38. 避免内存泄漏:在 useEffect 的 cleanup 中调用 dotLottie?.destroy()。
  39. 避免事件监听泄漏:添加监听后,cleanup 中必须 removeEventListener 同一个 handler。
  40. 当导出 JSON 超过 500KB 时,简化 After Effects 合成,减少图层和关键帧。
  41. 当导出 JSON 超过 500KB 时,使用 dotLottie 格式压缩。
  42. 当导出 JSON 超过 500KB 时,检查 Bodymovin 导出设置,不需要时关闭 'Include expressions'。
  43. 当导出 JSON 超过 500KB 时,导出前移除未使用资源。
  44. 当导出 JSON 超过 500KB 时,用 https://lottiefiles.com/tools/lottie-editor 优化。
  45. 解决动画卡顿:从 SVG 切换到 Canvas 渲染器。
  46. 解决动画卡顿:用 DotLottieWorker 做 web worker 渲染。
  47. 解决动画卡顿:在 After Effects 中减少图层、简化形状。
  48. 解决动画卡顿:移动端降低 devicePixelRatio。
  49. 解决动画卡顿:避免同时动画太多属性。
  50. 解决路径/URL 问题:本地打包优先 import animationData from './animation.json' 并传 animationData。
  51. 解决路径/URL 问题:外部 URL 用 src,并确保服务端有 CORS 头。
  52. 解决路径/URL 问题:Next.js 放到 public/ 目录,并用 src='/animations/animation.lottie'。
  53. 处理 After Effects 导出兼容性:注意 Lottie 不支持图层效果(drop shadows、glows),改用形状图层。
  54. 处理 After Effects 导出兼容性:混合模式仅有限支持。
  55. 处理 After Effects 导出兼容性:3D 图层不支持。
  56. 处理 After Effects 导出兼容性:表达式仅部分支持。
  57. 处理 After Effects 导出兼容性:轨道遮罩仅部分支持。
  58. 处理 After Effects 导出兼容性:尽早频繁测试导出。
  59. 处理 After Effects 导出兼容性:导出前用 LottieFiles preview 预览。
  60. 处理 After Effects 导出兼容性:检查 Bodymovin 兼容性列表 https://airbnb.io/lottie/#/supported-features。
  61. 处理 After Effects 导出兼容性:尽可能把效果转换为形状。
  62. 使用 scripts/generate_lottie_component.py 生成 React/Vue/Svelte Lottie 组件样板。
  63. 使用 scripts/optimize_lottie.py 优化 Lottie JSON 文件大小。
  64. 查阅 references/api_reference.md 获取 lottie-web、lottie-react、dotlottie-web 完整 API。
  65. 查阅 references/after_effects_export.md 获取 After Effects 导出指南。
  66. 查阅 references/performance_guide.md 获取详细性能优化策略。
  67. 使用 assets/starter_lottie/ 作为完整 React + Vite + Lottie starter template。
  68. 参考 assets/examples/ 中的真实 Lottie 动画模式和用例。
  69. 与 gsap-scrolltrigger 技能配合做滚动驱动 Lottie 动画同步页面滚动。
  70. 与 motion-framer 技能配合,用 Framer Motion 包裹 Lottie 做布局动画。
  71. 与 animated-component-libraries 技能配合,使用可能内置 Lottie 动画的预构建组件。
  72. 与 threejs-webgl 技能配合处理超出 Lottie 2D 能力的 3D 动画。
  73. 与 react-three-fiber 技能配合处理复杂 3D 动画场景替代方案。

适用边界 · 注意事项