Animate.css 的 80+ 个预设覆盖了常见场景,但实际项目中总有一些特殊需求:
filter: blur() + opacity)color 或 box-shadow(Animate.css 不涉及这些属性)@keyframes slideInFromTopRight { from { opacity: 0; transform: translate3d(50%, -100%, 0); } to { opacity: 1; transform: translate3d(0, 0, 0); } } .animate__slideInFromTopRight { animation-name: slideInFromTopRight; }
加上 animate__animated 基础类就可以使用:
<div class="animate__animated animate__slideInFromTopRight">自定义动画</div>
@keyframes springIn { 0% { opacity: 0; transform: translate3d(-100%, 0, 0) scale3d(0.5, 0.5, 0.5); } 60% { opacity: 1; transform: translate3d(10%, 0, 0) scale3d(1.05, 1.05, 1.05); } 80% { transform: translate3d(-5%, 0, 0) scale3d(0.98, 0.98, 0.98); } 100% { transform: translate3d(0, 0, 0) scale3d(1, 1, 1); } } .animate__springIn { animation-name: springIn; }
这个动画模拟了弹簧效果:先从左侧快速冲入,过冲到右侧 10%,回弹到左侧 5%,最终稳定。60/80/100 的帧百分比决定了回弹的节奏。
@keyframes blurIn { from { opacity: 0; filter: blur(20px); transform: scale3d(1.2, 1.2, 1.2); } to { opacity: 1; filter: blur(0); transform: scale3d(1, 1, 1); } } .animate__blurIn { animation-name: blurIn; }
注意:filter: blur() 触发的是 Paint 阶段(部分浏览器在 Composite),不像 transform 和 opacity 那样纯 GPU。如果动画元素是大面积区域,性能可能不如纯 transform 动画。但对于小元素(按钮、图标),影响可忽略。
保持与 Animate.css 一致的命名规范,方便团队理解:
animate__{动作}{方向}
fadeIn、slideIn、zoomIn、springInUp、Down、Left、Right自定义动画也应该加 animate__ 前缀,保持命名空间一致。
如果你通过 Sass 源码引入 Animate.css,可以把自定义动画放在同一个 Sass 文件中:
// animations.scss @import 'animate.css/src/base/base'; @import 'animate.css/src/utilities/delays'; @import 'animate.css/src/utilities/duration'; @import 'animate.css/src/utilities/repeat'; @import 'animate.css/src/animations/fading_entrances/fadeInUp'; @import 'animate.css/src/animations/zoom_entrances/zoomIn'; // 自定义动画 @keyframes springIn { 0% { opacity: 0; transform: translate3d(-100%, 0, 0) scale3d(0.5, 0.5, 0.5); } 60% { opacity: 1; transform: translate3d(10%, 0, 0) scale3d(1.05, 1.05, 1.05); } 80% { transform: translate3d(-5%, 0, 0) scale3d(0.98, 0.98, 0.98); } 100% { transform: translate3d(0, 0, 0) scale3d(1, 1, 1); } } .animate__springIn { animation-name: springIn; }
创建一个 PostCSS 插件来自动合并自定义动画:
// postcss-animate-custom.js const postcss = require('postcss'); module.exports = postcss.plugin('postcss-animate-custom', function(opts) { return function(css) { // 在 .animate__animated 规则后面插入自定义 keyframes var customAnimations = opts.animations || []; customAnimations.forEach(function(anim) { var rule = postcss.rule({ selectors: ['.animate__' + anim.name], nodes: [ postcss.decl({ prop: 'animation-name', value: anim.name }) ] }); css.append(rule); }); }; });
最简单的方式:自定义动画放在独立的 CSS 文件中,在 Animate.css 之后引入。
styles/ ├── vendor/ │ └── animate.css.min.css # Animate.css 完整版 ├── animations/ │ └── custom-animations.css # 你的自定义动画 └── main.css # 项目样式
<link rel="stylesheet" href="vendor/animate.css.min.css"> <link rel="stylesheet" href="animations/custom-animations.css">
引入顺序很重要:自定义 CSS 在后面,可以覆盖 Animate.css 的默认值。
编写完自定义 @keyframes 后,用 Chrome DevTools 验证性能:
如果自定义动画中包含了 filter、box-shadow 等触发 Paint 的属性,考虑是否能用 transform 替代。
例如,把 box-shadow 动画改为 opacity + 固定 box-shadow:
/* 不推荐:box-shadow 动画触发 Paint */ @keyframes glowIn { from { box-shadow: 0 0 0 rgba(0,0,0,0); } to { box-shadow: 0 0 30px rgba(67, 97, 238, 0.6); } } /* 推荐:用伪元素 + opacity 实现类似效果 */ @keyframes glowPseudo { from { opacity: 0; transform: scale3d(0.8, 0.8, 0.8); } to { opacity: 1; transform: scale3d(1, 1, 1); } } .card::after { content: ''; position: absolute; inset: -10px; border-radius: inherit; background: radial-gradient(circle, rgba(67,97,238,0.3) 0%, transparent 70%); z-index: -1; }
自定义动画的缓动函数对"手感"影响很大。常用的 cubic-bezier 值:
/* 快速启动,缓慢结束(进入动画推荐) */ .entering { --animate-timing: cubic-bezier(0.0, 0.0, 0.2, 1); } /* 缓慢启动,快速结束(退出动画推荐) */ .exiting { --animate-timing: cubic-bezier(0.4, 0.0, 1, 1); } /* 平滑标准(Material Design 默认) */ .standard { --animate-timing: cubic-bezier(0.4, 0.0, 0.2, 1); } /* 弹簧效果(过冲) */ .spring { --animate-timing: cubic-bezier(0.175, 0.885, 0.32, 1.275); } /* 极度弹性(多过冲) */ .bouncy { --animate-timing: cubic-bezier(0.68, -0.55, 0.265, 1.55); }
用 cubic-bezier.com 可视化工具调试你自己的缓动曲线。