Skip to content

Animation 动画

使用 CSS 动画来为元素添加动画效果的实用工具。

ClassProperties

基本用法

Spin 旋转

animate-spin 实用程序添加到元素,如加载指示器,以添加线性旋转动画。

html
<button type="button" class="bg-indigo-500 ..." disabled>
  <svg class="animate-spin h-5 w-5 mr-3 ..." viewBox="0 0 24 24">
    <!-- ... -->
  </svg>
  Processing...
</button>

Ping 平

animate-ping 实用程序添加到元素中,使其像雷达脉冲或水波纹一样缩放和淡出 — 适用于通知徽章等内容。

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

Pulse 脉搏

animate-pulse 实用程序添加到使元素轻轻地淡入淡出 - 对于骨架加载器等东西非常有用。

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

Bounce 弹跳

animate-bounce 实用程序添加到使元素上下弹跳 - 对于诸如“向下滚动”指示器之类的东西非常有用。

html
<svg class="animate-bounce w-6 h-6 ...">
  <!-- ... -->
</svg>

偏好减少动作

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

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

有条件地应用

悬停、焦点和其他状态

Tailwind 允许您使用变体修饰符在不同状态下有条件地应用实用程序类。例如,使用 hover:animate-spin 只在悬停时应用 animate-spin 实用程序。

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

有关所有可用状态修饰符的完整列表,请查看悬停、焦点和其他状态文档。

断点和媒体查询

您还可以使用变体修饰符来针对响应式断点、深色模式、偏好减少动作等媒体查询进行定位。例如,使用 md:animate-spin 仅在中等屏幕尺寸及以上应用 animate-spin 实用程序。

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

要了解更多信息,请查看有关响应式设计深色模式其他媒体查询修饰符的文档。

使用自定义值

定制您的主题

动画本质上往往是高度项目特定的。我们默认包含的动画最好被视为有用的示例,并鼓励您定制动画以更好地满足您的需求。

默认情况下,Tailwind 提供了四种不同示例动画的实用程序,以及 animate-none 实用程序。您可以通过编辑 tailwind.config.js 文件中的 theme.animationtheme.extend.animation 来自定义这些值。

js
module.exports = {
  theme: {
    extend: {
      animation: {									
        'spin-slow': 'spin 3s linear infinite',		
      }												
    }
  }
}

要添加新的动画 @keyframes ,请使用您主题配置的 keyframes 部分:

js
module.exports = {
  theme: {
    extend: {
      keyframes: {										
        wiggle: {										
          '0%, 100%': { transform: 'rotate(-3deg)' },	
          '50%': { transform: 'rotate(3deg)' },			
        }												
      }													
    }
  }
}

您可以在主题配置的 animation 部分通过名称引用这些关键帧:

module.exports = {
  theme: {
    extend: {
      animation: {
        wiggle: 'wiggle 1s ease-in-out infinite',
      }
    }
  }
}

有关自定义默认主题的更多信息,请参阅主题自定义文档。

任意值

如果您需要使用一个与主题无关的 animation 值,可以使用方括号来根据任意值动态生成属性。

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

有关任意值支持的更多信息,请参阅任意值文档。

Released under the MIT License.