Skip to content

CSS Transition 不 Animating

快速摘要

你的 transition 是不 工作因为属性是 不 transitionable, duration 是 missing, or 值更改是 不 triggered.

为什么发生

  • transition 属性是 misspelled or 不 animatable (e.g., display)

  • Missing duration 值 (defaults 到 0s — 否 animation)

  • 属性值 做不 actually 更改之间状态

  • transition 是定义 on 错误元素 or 状态

最小示例

✗ 错误代码
/* Missing duration — defaults to 0s */
.box { transition: background; }
.box:hover { background: red; }

/* display is not animatable */
.box { transition: display 0.3s; }
✓ 修复代码
/* Transition on base element with duration */
.box {
  background: blue;
  transition: background 0.3s ease;
}
.box:hover { background: red; }

添加 duration (0.3s) 到 transition. 定义 transition on 基础 .box 规则, 不 on :hover. Use animatable 属性如 background, 不 display.

如何诊断

  • 检查 transition shorthand — 是 duration specified?

  • 验证属性是 animatable (不所有属性能 transition)

  • Inspect DevTools 到 see 如果属性值 更改之间状态

  • Ensure transition 是 on 基础元素, 不只是 :hover 状态

如何修复

  • Specify duration: transition: 颜色 0.3s ease

  • Use animatable 属性 (颜色, opacity, transform, 宽度, etc.)

  • 定义 transition on 基础元素, 不 pseudo-类

  • Use transform and opacity for best 性能

如何预防

  • 总是包含 duration in transition declarations

  • Prefer transform and opacity for smooth 60fps animations

  • 测试 transitions 通过切换状态 in DevTools

相关资源

相关术语

相关课程

← 返回语言