我想声明一个@utility
名为 的函数animate-repeat-{number}
,它的作用是在使用时修改一个变量;这个变量我把它绑定到所有动画的重复次数上。这样就成功了。
@utility animate-repeat-* {
--animate-repeat-count: --value(integer);
}
@utility animate-repeat-infinite {
--animate-repeat-count: infinite;
}
@theme
但是,当我在(或者如果它默认存在,如 animate-bounce)中声明动画时,.animate-bounce { ... }
该类通过变量接收动画:
.animate-bounce {
animation: var(--animate-bounce);
}
这里的问题是,即使我覆盖了的值--animate-bounce
,DevTools(F12)仍然将其突出显示为bounce 1s infinite
而不是bounce 1s var(--animate-repeat-count, infinite)
。
不起作用的示例@theme |
|
---|---|
不起作用的示例 TailwindCSS v4 Playground(带有@theme ) |
|
![]() |
![]() |
如果我在 中声明动画@utility
,问题在于默认动画@theme
会优先。用 !important 覆盖它就可以了。
自然,没有了!important
,theme
层就更强,我无法覆盖它;这就是它能与一起工作的原因!important
。
不工作 | 与……合作important |
---|---|
无效示例 TailwindCSS v4 Playground(无重要信息) | 成功示例 TailwindCSS v4 Playground(重要) |
![]() |
![]() |
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<style type="text/tailwindcss">
@utility animate-bounce {
animation: bounce 1s var(--animate-repeat-count, infinite) !important;
}
@utility animate-repeat-* {
--animate-repeat-count: --value(integer);
}
@utility animate-repeat-infinite {
--animate-repeat-count: infinite;
}
</style>
<div class="p-10 text-center">
<button class="mx-auto animate-bounce bg-green-300 px-8 py-2 animate-repeat-2">
Bounce animation repeated 2 times in 1s.<br>(It only works with !important because utilities are weaker than the theme layer.)
</button>
</div>
如何才能使在默认情况下正确声明的动画@theme
工作,而无需使用!important?我希望以下代码的工作方式与三个共享游乐场中的最后一段代码相同:
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<style type="text/tailwindcss">
@theme {
--animate-bounce: bounce 1s var(--animate-repeat-count, infinite);
}
@utility animate-repeat-* {
--animate-repeat-count: --value(integer);
}
@utility animate-repeat-infinite {
--animate-repeat-count: infinite;
}
</style>
<div class="p-10 text-center">
<button class="mx-auto animate-bounce bg-green-300 px-8 py-2 animate-repeat-2">
Bounce animation repeated 2 times in 1s. (Not working)
</button>
</div>