1. 过渡与动画
  2. 过渡属性

过渡与动画

transition-property

控制哪些 CSS 属性过渡的工具类。

ClassStyles
transition
transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, --tw-gradient-from, --tw-gradient-via, --tw-gradient-to, opacity, box-shadow, transform, translate, scale, rotate, filter, -webkit-backdrop-filter, backdrop-filter; transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */
transition-all
transition-property: all; transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */
transition-colors
transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, --tw-gradient-from, --tw-gradient-via, --tw-gradient-to; transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */
transition-opacity
transition-property: opacity; transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */
transition-shadow
transition-property: box-shadow; transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */
transition-transform
transition-property: transform, translate, scale, rotate; transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */
transition-none
transition-property: none;
transition-(<custom-property>)
transition-property: var(<custom-property>); transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */
transition-[<value>]
transition-property: <value>; transition-timing-function: var(--default-transition-timing-function); /* cubic-bezier(0.4, 0, 0.2, 1) */ transition-duration: var(--default-transition-duration); /* 150ms */

示例

基本示例

使用 transitiontransition-colors 等工具类来指定更改时应过渡哪些属性:

悬停按钮以查看预期效果

<button class="bg-blue-500 transition delay-150 duration-300 ease-in-out hover:-translate-y-1 hover:scale-110 hover:bg-indigo-500 ...">
保存更改
</button>

支持减少运动

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

<button class="transform transition hover:-translate-y-1 motion-reduce:transition-none motion-reduce:hover:transform-none ...">
<!-- ... -->
</button>

使用自定义值

Use the transition-[<value>] syntax to set the 过渡属性 based on a completely custom value:

<button class="transition-[height] ...">
<!-- ... -->
</button>

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

<button class="transition-(--my-properties) ...">
<!-- ... -->
</button>

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

响应式设计

Prefix a transition-property utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<button class="transition-none md:transition-all ...">
<!-- ... -->
</button>

Learn more about using variants in the variants documentation.