1. 过渡与动画
  2. 动画

过渡与动画

动画

用于通过 CSS 动画给元素添加动画的工具类。

ClassStyles
animate-spin
animation: var(--animate-spin); /* 旋转 1s 线性无限 */ @keyframes spin { to { transform: rotate(360deg); } }
animate-ping
animation: var(--animate-ping); /* 像雷达一样 ping 1s 套装 (0, 0, 0.2, 1) 无限 */ @keyframes ping { 75%, 100% { transform: scale(2); opacity: 0; } }
animate-pulse
animation: var(--animate-pulse); /* 脉冲 2s 套装 (0.4, 0, 0.6, 1) 无限 */ @keyframes pulse { 50% { opacity: 0.5; } }
animate-bounce
animation: var(--animate-bounce); /* 反弹 1s 无限 */ @keyframes bounce { 0%, 100% { transform: translateY(-25%); animation-timing-function: cubic-bezier(0.8, 0, 1, 1); } 50% { transform: none; animation-timing-function: cubic-bezier(0, 0, 0.2, 1); } }
animate-none
animation: none;
animate-(<custom-property>)
animation: var(<custom-property>);
animate-[<value>]
animation: <value>;

示例

添加旋转动画

使用 animate-spin 工具类为加载指示器等元素添加线性旋转动画:

<button type="button" class="bg-indigo-500 ..." disabled>
<svg class="mr-3 size-5 animate-spin ..." viewBox="0 0 24 24">
<!-- ... -->
</svg>
正在处理…
</button>

添加 ping 动画

使用 animate-ping 工具类使元素像雷达 ping 或水波一样缩放和消失——对于通知徽章等非常有用:

<span class="relative flex size-3">
<span class="absolute inline-flex h-full w-full animate-ping rounded-full bg-sky-400 opacity-75"></span>
<span class="relative inline-flex size-3 rounded-full bg-sky-500"></span>
</span>

添加脉冲动画

使用 animate-pulse 工具类使元素轻柔地淡入淡出——对于骨架加载器等非常有用:

<div class="mx-auto w-full max-w-sm rounded-md border border-blue-300 p-4">
<div class="flex animate-pulse space-x-4">
<div class="size-10 rounded-full bg-gray-200"></div>
<div class="flex-1 space-y-6 py-1">
<div class="h-2 rounded bg-gray-200"></div>
<div class="space-y-3">
<div class="grid grid-cols-3 gap-4">
<div class="col-span-2 h-2 rounded bg-gray-200"></div>
<div class="col-span-1 h-2 rounded bg-gray-200"></div>
</div>
<div class="h-2 rounded bg-gray-200"></div>
</div>
</div>
</div>
</div>

添加反弹动画

使用 animate-bounce 工具类使元素上下反弹——对于“向下滚动”指示器等非常有用:

<svg class="size-6 animate-bounce ...">
<!-- ... -->
</svg>

支持减少运动

在用户指定希望减少运动的情况下,可以使用 motion-safemotion-reduce 变体有条件地应用动画和过渡:

<button type="button" class="bg-indigo-600 ..." disabled>
<svg class="mr-3 size-5 motion-safe:animate-spin ..." viewBox="0 0 24 24">
<!-- ... -->
</svg>
正在处理
</button>

使用自定义值

Use the animate-[<value>] syntax to set the animation based on a completely custom value:

<div class="animate-[wiggle_1s_ease-in-out_infinite] ...">
<!-- ... -->
</div>

For CSS variables, you can also use the animate-(<custom-property>) syntax:

<div class="animate-(--my-animation) ...">
<!-- ... -->
</div>

This is just a shorthand for animate-[var(<custom-property>)] that adds the var() function for you automatically.

响应式设计

Prefix an animation utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<div class="animate-none md:animate-spin ...">
<!-- ... -->
</div>

Learn more about using variants in the variants documentation.

自定义你的主题

Use the --animate-* theme variables to customize the animation utilities in your project:

@theme {
--animate-wiggle: wiggle 1s ease-in-out infinite;
@keyframes wiggle {
0%,
100% {
transform: rotate(-3deg);
}
50% {
transform: rotate(3deg);
}
}
}

Now the animate-wiggle utility can be used in your markup:

<div class="animate-wiggle">
<!-- ... -->
</div>

Learn more about customizing your theme in the theme documentation.