animejs、gsap 和 velocity-animate 都是用于在网页上创建动画的 JavaScript 库。gsap 是功能最强大的商业级引擎,适合复杂交互;animejs 主打轻量级和现代语法,适合简单动效和 SVG;velocity-animate 是早期的流行库,但现在已停止维护,属于遗留技术。它们都能操作 CSS 属性,但在性能、API 设计和长期支持上有很大区别。
在构建现代 Web 应用时,动画不仅仅是装饰,更是用户体验的核心部分。gsap、animejs 和 velocity-animate 都试图解决同一个问题:如何用代码控制 DOM 元素的运动。但它们的实现思路、维护状态和适用场景截然不同。本文将从架构师的角度,对比它们在实际工程中的表现。
API 的设计决定了代码的可读性和维护成本。这三者采用了不同的方式来定义动画。
gsap 使用静态方法调用,链式操作非常灵活。
gsap.to() 或 gsap.from() 直接定义。// gsap: 静态方法调用
gsap.to(".box", { duration: 1, x: 100, rotation: 360 });
animejs 使用单一函数入口,配置对象集中管理。
anime() 函数包裹所有配置。// animejs: 单一函数配置
anime({
targets: '.box',
translateX: 100,
rotate: '1turn',
duration: 1000
});
velocity-animate 依赖 jQuery 风格或原生元素调用。
$ 对象上或直接调用 Velocity。// velocity-animate: jQuery 风格调用
$(".box").velocity({ left: "100px" }, 1000);
// 或原生用法
Velocity(document.querySelector(".box"), { left: "100px" }, 1000);
当多个动画需要按顺序播放或同步时,时间轴(Timeline)能力至关重要。
gsap 拥有业界最强的时间轴系统。
// gsap: 强大时间轴
const tl = gsap.timeline();
tl.to(".box", { x: 100 })
.to(".circle", { scale: 2 }, "-=0.5"); // 重叠 0.5 秒
animejs 提供基础的时间轴功能。
anime.timeline() 创建。// animejs: 基础时间轴
const tl = anime.timeline();
tl.add({ targets: '.box', translateX: 100 })
.add({ targets: '.circle', scale: 2 }, '-=500');
velocity-animate 缺乏原生时间轴支持。
RunSequence 或手动回调串联。// velocity-animate: 手动序列
Velocity.RunSequence([
{ e: ".box", p: { left: "100px" }, o: { duration: 1000 } },
{ e: ".circle", p: { scale: 2 }, o: { duration: 1000 } }
]);
动画的流畅度取决于库如何更新 DOM 和请求帧。
gsap 使用高度优化的渲染引擎。
// gsap: 自动优化 transforms
gsap.to(".box", { x: 100, willChange: "transform" });
animejs 性能表现良好,适合大多数场景。
requestAnimationFrame 循环。// animejs: SVG 路径优化
anime({
targets: 'path',
strokeDashoffset: [anime.setDashoffset, 0]
});
velocity-animate 在旧浏览器上表现不错,但现代环境下无优势。
// velocity-animate: 基础缓动
Velocity(".box", "transition.fadeIn", { duration: 500 });
这是架构选型中最关键的一点。库是否有人维护直接关系到项目的长期安全。
gsap 由专业团队持续维护。
// gsap: 现代插件生态
gsap.registerPlugin(ScrollTrigger);
gsap.to(".box", { scrollTrigger: ".section" });
animejs 社区活跃,稳定更新。
// animejs: 社区支持
// 通过 npm 正常安装更新
import anime from 'animejs';
velocity-animate 已停止主要维护。
// velocity-animate: 不推荐新用途
// 仅建议在维护旧系统时暂时保留
import Velocity from 'velocity-animate';
| 特性 | gsap | animejs | velocity-animate |
|---|---|---|---|
| API 风格 | 链式调用,静态方法 | 对象配置,函数式 | jQuery 风格,命令式 |
| 时间轴 | ✅ 强大,支持嵌套 | ✅ 基础,支持偏移 | ❌ 弱,需手动序列 |
| SVG 支持 | ✅ 优秀 (插件) | ✅ 原生优秀 | ⚠️ 基础支持 |
| 维护状态 | ✅ 活跃,商业支持 | ✅ 活跃,社区支持 | ❌ 停止维护,遗留 |
| 学习曲线 | 中等,文档详尽 | 低,上手快 | 低,但资料过时 |
| 适用场景 | 复杂交互,商业项目 | 轻量动效,SVG | 旧项目维护 |
gsap 是工业级标准 🏭。如果你在做复杂的营销页面、交互应用或者需要确保动画在所有设备上流畅,它是首选。虽然需要学习成本,但长期回报高。
animejs 是轻量级利器 🔦。适合个人项目、小型组件库或者对包体积敏感的场景。它的 SVG 动画能力尤其出色,代码写起来很直观。
velocity-animate 是历史遗留物 🕸️。除非你正在维护一个五年前的老系统,否则不要在新代码中使用它。现代浏览器原生性能和更好的库已经取代了它的地位。
最终建议:对于 2024 年及以后的新项目,请在 gsap 和 animejs 之间选择。根据复杂度决定 —— 复杂选 GSAP,简单选 Anime。
避免在新项目中使用 velocity-animate。虽然它曾经很流行,但目前已停止主要维护,缺乏对新特性的支持。除非是在维护旧代码库,否则应优先评估 gsap 或 animejs 作为替代方案。
选择 animejs 如果你需要轻量级的解决方案,或者主要操作 SVG 路径动画。它的 API 简洁现代,无需绑定 jQuery,适合中小型项目或只需要简单过渡效果的场景。打包体积小,集成成本低。
选择 gsap 如果你的项目需要复杂的时间轴控制、大量的动画序列或者商业级的稳定性。它拥有最完善的插件生态(如滚动触发),适合大型应用和需要精细控制的场景。虽然体积稍大,但性能优化极好。
WhatsApp, Tumblr, Windows, Samsung, Uber, and thousands of other companies rely on Velocity. Visit Libscore.com to see which sites use Velocity on their homepage.
Announcement: https://fabric.io/blog/introducing-the-velocityreact-library
Repo: https://github.com/twitter-fabric/velocity-react
NPM: https://www.npmjs.com/package/velocity-react
<script src="//cdn.jsdelivr.net/npm/velocity-animate@1.5/velocity.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.5.2/velocity.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/velocity-animate@1.5/velocity.ui.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.5.2/velocity.ui.min.js"></script>
Please note that JSDelivr will automatically supply the latest release, while CloudFlare needs to ask for a specific version.
npm: npm install velocity-animate
bower: bower install velocity
Ask on StackOverflow (make sure you add the [velocity.js] and [javascript] tags).
background:["rgba(red,0.1)", "blue"]).
High resolution timers (animations should be slightly smoother).velocity.js. Read VelocityJS.org/#dependencies.stop now stops animations immediately (instead of only clearing the remainder of the animation queue). No other backwards-incompatible changes were made.====
MIT License. © Julian Shapiro (http://twitter.com/shapiro).
Stripe sponsors Velocity's development.
BrowserStack provides testing services.