简体   繁体   English

顺风背景渐变过渡

[英]Tailwind background gradient transition

Does Tailwind CSS allow transitions of gradients ie changing the 'from' or 'to' color so that the gradient of either color changes by a transition? Tailwind CSS 是否允许渐变过渡,即更改“从”“到”颜色,以便任一颜色的渐变通过过渡而改变?

What I have tried:我试过的:

<button class="transition duration-500 ease-in-out bg-gradient-to-t from-black to-white hover:to-red-500">
    Hover me
</button>

As chojinicki already pointed out, it is not possible without any workarounds, especially without adding extensions to your config file.正如 chojinicki 已经指出的那样,没有任何变通方法是不可能的,尤其是在没有向配置文件添加扩展名的情况下。 Because I needed the exact same for my project, I created my own workaround for it.因为我的项目需要完全相同,所以我为它创建了自己的解决方法。

You have to double the background size of the element and transition the background position using transition-all to get the desired effect.您必须将元素的背景大小加倍并使用transition-all转换背景 position 以获得所需的效果。 Note that you require the via- gradient stop.请注意,您需要via-梯度停止。

Tailwind Play: https://play.tailwindcss.com/XFQDCOKQ8L顺风玩: https://play.tailwindcss.com/XFQDCOKQ8L

<button className="transition-all duration-500 bg-gradient-to-t to-white via-black from-red-500 bg-size-200 bg-pos-0 hover:bg-pos-100">
    Hover me
</button>

tailwind.config.js tailwind.config.js

module.exports = {
    // ...
    theme: {
        extend: {
            backgroundSize: {
                'size-200': '200% 200%',
            },
            backgroundPosition: {
                'pos-0': '0% 0%',
                'pos-100': '100% 100%',
            },
        },
    }
};

Unfortunately, it is very limited and doesn't exactly work with your exact example provided, however this is the closest you can get.不幸的是,它非常有限,并且不能完全与您提供的确切示例一起使用,但是这是您可以获得的最接近的示例。

Short answer No , but not because of lack this functionality in Tailwind but rather in CSS itself.简短回答,但不是因为 Tailwind 中缺少此功能,而是因为 CSS 本身缺少此功能。 Reason for that is probably performance issues - browser engine would have to render separate gradient for every frame of animation.原因可能是性能问题 - 浏览器引擎必须为 animation 的每一帧呈现单独的渐变。

This is common question: Use CSS3 transitions with gradient backgrounds这是一个常见问题: 使用带有渐变背景的 CSS3 过渡

You can only try to use some workaround (depends what exactly effect you expect) like background position, opacity etc (examples in linked question and plenty tutorials online).您只能尝试使用一些解决方法(取决于您期望的确切效果),例如背景 position、不透明度等(链接问题中的示例和大量在线教程)。 If something works for you - just extract this as Tailwind utility if you need this in multiple places.如果某些东西对您有用 - 如果您在多个地方需要它,只需将其提取为 Tailwind 实用程序。

https://tailwindcss.com/docs/adding-new-utilities https://tailwindcss.com/docs/adding-new-utilities

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM