首页 关于 文章 作品集 工具 联系
返回文章列表

CSS 动画与过渡:从入门到进阶

动画是让页面"活"起来的关键。一个精心设计的微交互动画能让用户体验提升一个档次。CSS 原生提供了 transition 和 animation 两大机制,大多数场景下不需要引入 JavaScript 动画库。这篇文章从基础概念到性能优化,帮你彻底掌握 CSS 动画。

一、Transition:简单的状态过渡

Transition(过渡)用于在两个状态之间创建平滑的变化。它需要两个条件:触发的属性变化定义好的过渡参数

.button {
  background: #c9a96e;
  color: #fff;
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  /* transition 属性:要过渡的属性 | 持续时间 | 时间函数 | 延迟 */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.button:hover {
  background: #b8943d;
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(201, 169, 110, 0.3);
}

.button:active {
  transform: translateY(0);
}

1.1 时间函数(Timing Function)

时间函数决定了动画的速度曲线。除了预设的 lineareaseease-inease-outease-in-out,还可以用 cubic-bezier() 自定义:

/* 常用贝塞尔曲线 */
.element {
  /* 弹性效果(轻微回弹)*/
  transition: all 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55);

  /* Material Design 标准曲线 */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

  /* 快速启动,慢速结束 */
  transition: all 0.5s cubic-bezier(0.0, 0.0, 0.2, 1);
}

1.2 哪些属性可以过渡?

只有可计算中间值的属性才能过渡:

  • 颜色:color, background-color, border-color, opacity
  • 尺寸:width, height, max-width, max-height, margin, padding
  • 位置:top, left, right, bottom, transform
  • 视觉:box-shadow, border-radius, outline
  • 滤镜:filter (blur, brightness, contrast...)
/* 卡片悬浮效果 */
.card {
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
}

/* 输入框聚焦效果 */
.input {
  border: 2px solid #ddd;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}
.input:focus {
  border-color: #c9a96e;
  box-shadow: 0 0 0 4px rgba(201, 169, 110, 0.15);
  outline: none;
}

二、Animation:关键帧动画

Animation 比 Transition 更强大——它不需要触发条件,可以自动播放、循环、暂停,并且通过 @keyframes 定义多步骤动画。

2.1 基本语法

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in-element {
  /* animation: name | duration | timing-function | delay | iteration-count | direction | fill-mode */
  animation: fadeInUp 0.6s ease-out forwards;
}

/* 也用百分比定义更精细的控制 */
@keyframes bounce {
  0%   { transform: translateY(0); }
  20%  { transform: translateY(-20px); }
  40%  { transform: translateY(0); }
  60%  { transform: translateY(-10px); }
  80%  { transform: translateY(0); }
  100% { transform: translateY(-5px); }
}

.bounce {
  animation: bounce 1s ease infinite;
}

2.2 animation 属性详解

.animated {
  animation-name: slideIn;      /* 动画名称 */
  animation-duration: 1s;        /* 持续时间 */
  animation-timing-function: ease-in-out;  /* 时间函数 */
  animation-delay: 0.5s;         /* 延迟 */
  animation-iteration-count: 3;  /* 播放次数(infinite = 无限)*/
  animation-direction: alternate; /* 方向(alternate = 来回交替)*/
  animation-fill-mode: forwards; /* 填充模式(forwards = 保持结束状态)*/
  animation-play-state: running; /* 播放状态(paused | running)*/
}

/* 简写 */
.animated {
  animation: slideIn 1s ease-in-out 0.5s 3 alternate forwards;
}

2.3 实用动画案例

/* 加载旋转动画 */
@keyframes spin {
  to { transform: rotate(360deg); }
}
.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #e5e7eb;
  border-top-color: #c9a96e;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* 脉冲呼吸灯效果 */
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%      { opacity: 0.7; transform: scale(1.05); }
}
.pulse-dot {
  width: 10px;
  height: 10px;
  background: #22c55e;
  border-radius: 50%;
  animation: pulse 2s ease-in-out infinite;
}

/* 文字逐字出现 */
@keyframes typewriter {
  from { width: 0; }
  to   { width: 100%; }
}
@keyframes blink {
  50% { border-color: transparent; }
}
.typewriter {
  overflow: hidden;
  white-space: nowrap;
  border-right: 2px solid #c9a96e;
  animation: typewriter 3s steps(20) forwards, blink 0.8s step-end infinite;
}

/* 渐入上移(滚动触发场景常用)*/
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.scroll-reveal {
  opacity: 0;
  animation: fadeInUp 0.6s ease-out forwards;
}
.scroll-reveal:nth-child(2) { animation-delay: 0.1s; }
.scroll-reveal:nth-child(3) { animation-delay: 0.2s; }
.scroll-reveal:nth-child(4) { animation-delay: 0.3s; }

三、性能优化:60fps 流畅动画

动画卡顿是最影响用户体验的问题之一。理解浏览器渲染流程是优化性能的基础:

浏览器渲染流程:JavaScript → Style → Layout → Paint → Composite

触发不同流程的属性代价不同:

  • Composite(合成):transform, opacity — 性能最好,GPU 加速
  • Paint(绘制):color, background, box-shadow, border — 性能中等
  • Layout(布局):width, height, margin, padding, top, left — 性能最差,触发回流
/* ❌ 差:触发 Layout 回流 */
.bad-animation {
  transition: width 0.3s, height 0.3s, left 0.3s, top 0.3s;
}

/* ✅ 好:只触发 Composite 合成 */
.good-animation {
  transition: transform 0.3s, opacity 0.3s;
}

/* 使用 will-change 提示浏览器提前优化 */
.animated-element {
  will-change: transform, opacity;
}

/* 注意:will-change 不要滥用 */
/* 只在动画即将开始时添加,动画结束后移除 */
.animated-element.animating {
  will-change: transform;
}

3.1 prefers-reduced-motion

尊重用户的动画偏好——有些人会因为动画感到头晕:

/* 系统级动画偏好 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* JS 中检测 */
const prefersReduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!prefersReduced) {
  // 执行动画
}

四、Transition vs Animation:如何选择?

  • Transition:A→B 两个状态之间的过渡,需要触发条件(hover、focus、class 变化)
  • Animation:多步骤、自动播放、循环、更复杂的动画序列
/* Transition 适合:交互反馈 */
.button { transition: transform 0.2s, box-shadow 0.2s; }
.button:hover { transform: scale(1.05); }

/* Animation 适合:装饰性、加载、入场动画 */
.loading { animation: spin 1s linear infinite; }
.hero-title { animation: fadeInUp 0.8s ease-out; }

CSS 动画是前端开发中最容易上手但最容易被忽视深度的领域。掌握 Transition 处理交互反馈,用 Animation 实现复杂动效,再加上性能优化的意识,你就能创建出既流畅又优雅的用户体验。记住:好的动画是感觉不到存在的——它让交互变得自然,而不是引人注意。