第 1 节 概述
好的动效像无声的导游:
- 按钮悬停变暗,告诉你"我可点击"
- 页面切换平滑过渡,让你知道"我在哪里"
- 加载时旋转图标,告诉你"请稍等"
Tailwind 用三个层面来创造动效:变换(transform)、过渡(transition)、动画(animation)。
变换改变元素的形态——放大缩小、旋转、移动。
2.1 缩放
1
2
3
4
5
6
7
| <button class="scale-100 hover:scale-105 bg-blue-600 text-white px-6 py-3 rounded-lg">
悬停放大 5%
</button>
<button class="scale-100 active:scale-95 bg-blue-600 text-white px-6 py-3 rounded-lg">
点击略微缩小,模拟按下
</button>
|
| 类名 | 缩放比例 |
|---|
scale-95 | 缩小到 95% |
scale-100 | 原始大小 |
scale-105 | 放大到 105% |
scale-110 | 放大到 110% |
scale-x-150 | 水平方向拉伸 1.5 倍 |
scale-y-50 | 垂直方向压缩一半 |
2.2 旋转
1
2
3
4
5
6
| <div class="rotate-0 hover:rotate-12 transition">
悬停旋转 12 度
</div>
<!-- 加载中 -->
<div class="animate-spin">⏳</div>
|
2.3 平移
1
2
3
| <button class="translate-y-0 hover:-translate-y-1 bg-blue-600 text-white px-6 py-3 rounded-lg shadow-lg transition">
悬停向上移动 4px
</button>
|
技巧
按钮悬停的组合拳:hover:-translate-y-1 hover:shadow-xl。上移 + 加深阴影,模拟被"提起"的物理感。
2.4 旋转变换中心
变换默认以元素中心为原点,可以用 origin-* 改变:
1
2
| <div class="origin-top-left rotate-6">以左上角为轴旋转</div>
<div class="origin-bottom-right rotate-6">以右下角为轴旋转</div>
|
第 3 节 过渡(Transition)
变换本身是瞬间完成的,看起来生硬。过渡让变化过程变得平滑。
3.1 过渡属性
1
2
3
4
5
6
7
8
9
| <button class="bg-blue-600 hover:bg-blue-700 transition-colors duration-300">
背景色在 300ms 内渐变
</button>
<div class="opacity-100 hover:opacity-70 transition-opacity duration-200">
透明度在 200ms 内渐变
</div>
<img class="grayscale hover:grayscale-0 transition-all duration-500" src="photo.jpg" />
|
| 过渡类 | 作用的属性 |
|---|
transition | 所有可过渡属性 |
transition-colors | 颜色相关(背景、边框、文字) |
transition-opacity | 透明度 |
transition-shadow | 阴影 |
transition-transform | 变换(transform) |
transition-none | 无过渡 |

3.2 持续时间
1
2
3
4
5
| <div class="transition duration-75">75ms 极快</div>
<div class="transition duration-200">200ms 快</div>
<div class="transition duration-300">300ms 正常</div>
<div class="transition duration-500">500ms 慢</div>
<div class="transition duration-1000">1000ms 很慢</div>
|
3.3 缓动函数
缓动决定动画的快慢节奏:
1
2
3
4
| <div class="transition ease-linear">匀速,机械感</div>
<div class="transition ease-in">慢入快出,物体下落感</div>
<div class="transition ease-out">快入慢出,物体弹起感</div>
<div class="transition ease-in-out">两头慢中间快,最自然</div>
|
技巧
界面 UI 动效用 ease-out(快进慢出)最自然。ease-in-out 适合轮播图等循环动画。linear 适合旋转加载等机械运动。
3.4 延迟
1
2
3
| <div class="transition delay-150">延迟 150ms 后开始</div>
<div class="transition delay-300">延迟 300ms 后开始</div>
<div class="transition delay-700">延迟 700ms 后开始</div>
|
延迟适合做"先后入场"效果:比如菜单项依次出现。
3.5 常用组合
按钮悬停的"黄金组合":
1
2
3
4
5
6
7
| <button class="transform transition-all duration-200 ease-out
hover:-translate-y-1 hover:shadow-xl
active:scale-95
bg-blue-600 hover:bg-blue-700 text-white px-8 py-4 rounded-xl
shadow-lg shadow-blue-600/30">
立即购买
</button>
|
解释:
transform — 启用变换transition-all duration-200 ease-out — 所有属性 200ms 过渡,快进慢出hover:-translate-y-1 hover:shadow-xl — 悬停时上移、加深阴影active:scale-95 — 点击时略微缩小
第 4 节 组悬停(Group Hover)
当鼠标悬停在父元素上时,让子元素也发生变化。这种模式很常见——卡片整体可交互时,里面的按钮或箭头在悬停时移动。
1
2
3
4
5
6
7
| <div class="group p-6 bg-white rounded-2xl border border-gray-100 shadow-md hover:shadow-lg cursor-pointer">
<h3 class="text-xl font-semibold text-gray-900">卡片标题</h3>
<p class="mt-2 text-gray-500">卡片内容</p>
<span class="mt-4 inline-block text-blue-600 group-hover:translate-x-1 transition-transform">
了解更多 →
</span>
</div>
|
原理很简单:
- 父元素加
group 类 - 子元素用
group-hover:* 代替 hover:*
第 5 节 内置动画
Tailwind 预置了几个常用动画:
| 类名 | 效果 | 适用场景 |
|---|
animate-spin | 连续旋转 | 加载状态 |
animate-ping | 脉冲扩散 | 新消息提示 |
animate-pulse | 淡入淡出 | 骨架屏、加载占位 |
animate-bounce | 上下弹跳 | 提示引导、滚动指示 |
1
2
3
4
5
6
7
8
9
10
11
| <!-- 加载按钮 -->
<button disabled class="bg-blue-600 text-white px-6 py-3 rounded-lg opacity-70">
<span class="inline-block animate-spin">⏳</span> 加载中...
</button>
<!-- 红点提示 -->
<span class="absolute -top-1 -right-1 w-3 h-3 bg-red-500 rounded-full animate-ping"></span>
<!-- 骨架屏加载 -->
<div class="animate-pulse bg-gray-200 h-4 rounded w-3/4"></div>
<div class="animate-pulse bg-gray-200 h-4 rounded w-1/2 mt-3"></div>
|
第 6 节 自定义动画(v4)
在 v4 中,自定义动画在 @theme 中定义 keyframes 和 animation:
1
2
3
4
5
6
7
8
9
10
11
12
13
| @import "tailwindcss";
@theme {
--animate-fade-in: fade-in 0.5s ease-out;
@keyframes fade-in {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
--animate-slide-in: slide-in 0.3s ease-out;
@keyframes slide-in {
from { opacity: 0; transform: translateX(-20px); }
to { opacity: 1; transform: translateX(0); }
}
}
|
定义后就可以在 HTML 中使用了:animate-fade-in、animate-slide-in。
第 7 节 实战:加入动效的产品页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
| <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>星光科技 · 智能灯</title>
<script src="https://unpkg.com/@tailwindcss/browser@4"></script>
</head>
<body>
<nav class="fixed top-0 inset-x-0 bg-white/70 backdrop-blur-md border-b border-gray-100/50 z-50">
<div class="max-w-5xl mx-auto flex items-center justify-between px-6 h-16">
<span class="text-xl font-bold text-gray-900">星光科技</span>
<div class="flex gap-8 text-gray-600">
<a href="#" class="hover:text-gray-900 transition-colors">产品</a>
<a href="#" class="hover:text-gray-900 transition-colors">特性</a>
<a href="#" class="hover:text-gray-900 transition-colors">用户评价</a>
<a href="#" class="hover:text-gray-900 transition-colors">联系我们</a>
</div>
</div>
</nav>
<main class="max-w-5xl mx-auto px-6 pt-32 pb-16">
<!-- Hero -->
<div class="bg-gradient-to-br from-blue-50 via-white to-purple-50 rounded-3xl p-16 text-center">
<h1 class="text-5xl font-extrabold text-gray-900 tracking-tight">
智能台灯 <span class="text-transparent bg-clip-text bg-gradient-to-r from-blue-600 to-purple-600">Lumina</span>
</h1>
<p class="mt-6 text-xl text-gray-500 leading-relaxed max-w-2xl mx-auto">
光线随你而变。一盏懂你的台灯。
</p>
<button class="mt-8 px-8 py-4 bg-gradient-to-r from-blue-600 to-purple-600 text-white text-lg font-semibold rounded-xl shadow-lg shadow-blue-600/30
transform transition-all duration-200 ease-out
hover:-translate-y-1 hover:shadow-xl hover:shadow-blue-600/40
active:scale-95">
立即购买
</button>
</div>
<!-- 特性卡片 -->
<section class="mt-32">
<h2 class="text-3xl font-bold text-gray-900 text-center">为什么选择 Lumina</h2>
<div class="mt-12 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8">
<div class="group p-6 bg-white/80 backdrop-blur-sm rounded-2xl border border-gray-100 shadow-md
transition-all duration-200 hover:shadow-xl hover:-translate-y-1">
<div class="w-12 h-12 bg-gradient-to-br from-blue-100 to-blue-50 rounded-xl flex items-center justify-center
transition-transform duration-200 group-hover:scale-110">
<span class="text-2xl">🧠</span>
</div>
<h3 class="mt-4 text-xl font-semibold text-gray-900">AI 调光</h3>
<p class="mt-2 text-gray-500 leading-relaxed">内置传感器实时感知环境光线,自动调整亮度和色温。</p>
</div>
<div class="group p-6 bg-white/80 backdrop-blur-sm rounded-2xl border border-gray-100 shadow-md
transition-all duration-200 hover:shadow-xl hover:-translate-y-1">
<div class="w-12 h-12 bg-gradient-to-br from-green-100 to-green-50 rounded-xl flex items-center justify-center
transition-transform duration-200 group-hover:scale-110">
<span class="text-2xl">👁️</span>
</div>
<h3 class="mt-4 text-xl font-semibold text-gray-900">护眼模式</h3>
<p class="mt-2 text-gray-500 leading-relaxed">无频闪、低蓝光,通过德国莱茵 TÜV 护眼认证。</p>
</div>
<div class="group p-6 bg-white/80 backdrop-blur-sm rounded-2xl border border-gray-100 shadow-md
transition-all duration-200 hover:shadow-xl hover:-translate-y-1">
<div class="w-12 h-12 bg-gradient-to-br from-purple-100 to-purple-50 rounded-xl flex items-center justify-center
transition-transform duration-200 group-hover:scale-110">
<span class="text-2xl">🎤</span>
</div>
<h3 class="mt-4 text-xl font-semibold text-gray-900">语音控制</h3>
<p class="mt-2 text-gray-500 leading-relaxed">支持 Siri、小爱同学、Google Assistant 语音指令。</p>
</div>
</div>
</section>
<!-- 用户评价 -->
<section class="mt-32">
<h2 class="text-3xl font-bold text-gray-900 text-center">用户评价</h2>
<div class="mt-12 divide-y divide-gray-100">
<div class="py-6 flex gap-4">
<img class="w-12 h-12 rounded-full grayscale hover:grayscale-0 transition-all duration-300" src="https://i.pravatar.cc/48?img=1" alt="用户" />
<div>
<p class="font-semibold text-gray-900">设计师小王</p>
<p class="mt-1 text-gray-500">"用了两周,眼睛舒服多了。AI 调光真的很智能。"</p>
</div>
</div>
<div class="py-6 flex gap-4">
<img class="w-12 h-12 rounded-full grayscale hover:grayscale-0 transition-all duration-300" src="https://i.pravatar.cc/48?img=2" alt="用户" />
<div>
<p class="font-semibold text-gray-900">程序员大刘</p>
<p class="mt-1 text-gray-500">"晚上写代码的时候,灯光会自动变暖,不刺眼。"</p>
</div>
</div>
<div class="py-6 flex gap-4">
<img class="w-12 h-12 rounded-full grayscale hover:grayscale-0 transition-all duration-300" src="https://i.pravatar.cc/48?img=3" alt="用户" />
<div>
<p class="font-semibold text-gray-900">学生小林</p>
<p class="mt-1 text-gray-500">"期末复习全靠它了,灯光可以跟着时间变化。"</p>
</div>
</div>
</div>
</section>
<footer class="mt-32 pt-8 border-t border-gray-100 flex items-center justify-between text-sm text-gray-400">
<span>© 2026 星光科技</span>
<div class="flex gap-6">
<a href="#" class="hover:text-gray-600 transition-colors">隐私政策</a>
<a href="#" class="hover:text-gray-600 transition-colors">服务条款</a>
</div>
</footer>
</main>
</body>
</html>
|
这一版的动效清单:
- 导航链接:
hover:text-gray-900 transition-colors - 主按钮:
hover:-translate-y-1 hover:shadow-xl active:scale-95 加 transition-all duration-200 ease-out - 卡片:
hover:shadow-xl hover:-translate-y-1,悬停时浮起 - 卡片图标:
group-hover:scale-110,悬停卡片时图标放大 - 头像:
grayscale hover:grayscale-0 transition-all duration-300
本章小结
transform + transition 是动效的基础组合hover:*、active:* 是触发动效的方式group-hover:* 让父元素悬停时子元素动transition-{属性} duration-{时间} ease-{缓动} 三步控制动效细节- 内置动画
spin、ping、pulse、bounce 覆盖常见场景
动效加上了,页面交互很自然。但目前的页面在大屏幕上还行,手机上一看就乱了。下一篇来解决响应式布局。