Skip to content
全部文档

animation

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

快速参考

属性
animate-spinanimation: var(--animate-spin); /* spin 1s linear infinite */ @keyframes spin { to { transform: rotate(360deg); } }
animate-pinganimation: var(--animate-ping); /* ping 1s cubic-bezier(0, 0, 0.2, 1) infinite */ @keyframes ping { 75%, 100% { transform: scale(2); opacity: 0; } }
animate-pulseanimation: var(--animate-pulse); /* pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite */ @keyframes pulse { 50% { opacity: 0.5; } }
animate-bounceanimation: var(--animate-bounce); /* bounce 1s infinite */ @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-noneanimation: none;
animate-(<custom-property>)animation: var(<custom-property>);
animate-[<value>]animation: <value>;

示例

添加旋转动画

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

html
<!-- [!code classes:animate-spin] -->
<button type="button" class="bg-indigo-500 ..." disabled>
  <svg class="mr-3 size-5 animate-spin ..." viewBox="0 0 24 24">
    <!-- ... -->
  </svg>
  Processing…
</button>

添加 ping 动画

使用 animate-ping 工具类,让元素像雷达探测或水波纹一样缩放并淡出——适合通知徽章等场景:

html
<!-- [!code classes:animate-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 工具类,让元素轻轻淡入淡出——适合骨架屏加载等场景:

html
<!-- [!code classes: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 工具类,让元素上下弹跳——适合「向下滚动」指示器等场景:

html
<!-- [!code classes:animate-bounce] -->
<svg class="size-6 animate-bounce ...">
  <!-- ... -->
</svg>

支持减弱动态效果

如果用户已指定偏好减弱动态效果,可以使用 motion-safemotion-reduce 变体有条件地应用动画和过渡:

html
<!-- [!code classes:motion-safe:animate-spin] -->
<button type="button" class="bg-indigo-600 ..." disabled>
  <svg class="mr-3 size-5 motion-safe:animate-spin ..." viewBox="0 0 24 24">
    <!-- ... -->
  </svg>
  Processing
</button>

使用自定义值

使用 animate-[<value>] 等工具类,根据完全自定义的值设置动画:

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

对于 CSS 变量,也可以使用 animate-(<custom-property>) 语法:

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

这只是 animate-[var(<custom-property>)] 的简写,会自动为你加上 var() 函数。

响应式设计

使用 md: 等断点变体作为 animation 工具类的前缀,即可仅在中等及以上屏幕尺寸下应用该工具类:

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

了解如何使用变体,请参阅变体文档

自定义主题

使用 --animate-* 主题变量来自定义项目中的动画工具类:

css
dedent`
      @theme {
        
        --animate-wiggle: wiggle 1s ease-in-out infinite;

        @keyframes wiggle {
          0%,
          100% {
            transform: rotate(-3deg);
          }
          50% {
            transform: rotate(3deg);
          }
        }
      }
    `

现在可以在标记中使用 animate-wiggle 工具类:

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

了解如何自定义主题,请参阅主题文档

文档译文以 MIT 协议授权;原文版权归 Tailwind Labs。湘ICP备2026005453号-2