自定义动画开发:从零编写@keyframes并融入构建流程


自定义动画开发:从零编写@keyframes并融入构建流程

为什么需要自定义动画

Animate.css 的 80+ 个预设覆盖了常见场景,但实际项目中总有一些特殊需求:

  • 品牌要求特定的动画曲线(比如"弹簧回弹 1.5 倍过冲")
  • 特殊的位移方向(从右上角 45° 淡入)
  • 与其他 CSS 属性组合(比如 filter: blur() + opacity)
  • 动画中需要改变 color 或 box-shadow(Animate.css 不涉及这些属性)

从零编写一个 @keyframes

基本模板

@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 的帧百分比决定了回弹的节奏。

包含 blur 效果的动画

@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.css 一致的命名规范,方便团队理解:

animate__{动作}{方向} ​
  • 动作:fadeIn、slideIn、zoomIn、springIn
  • 方向(可选):Up、Down、Left、Right

自定义动画也应该加 animate__ 前缀,保持命名空间一致。

融入 Animate.css 构建流程

方案一:Sass 混合

如果你通过 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 插件来自动合并自定义动画:

// 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 文件

最简单的方式:自定义动画放在独立的 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 验证性能:

  1. Performance 面板录制动画
  2. 查看 Main 线程是否有 Layout/Paint 标记
  3. 如果有紫色(Layout)或绿色(Paint)标记,说明使用了非合成属性
  4. 检查 FPS 是否稳定在 60

如果自定义动画中包含了 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 可视化工具调试你自己的缓动曲线。

作者与出处
原作者:灏天文库
来源:平台策划编纂
整理人: 灏天文库整理
由灏天文库平台收录,内容或由平台用户上传,仅供学习交流
发布者: 作者: 灏天文库 转发
评论区 (0)
U