最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Make tailwind css color animate transition to always go on the same direction, from left to right - Stack Overflow

programmeradmin5浏览0评论

I have the following classes using tailwind 3.4.17:

bg-gradient-to-r from-pink-500 via-purple-500 to-blue-500 bg-[length:400%_100%] animate-[gradientMove_2s_linear_infinite]

with keyframes:

'0%': { backgroundPosition: '0% 50%' },
'50%': { backgroundPosition: '100% 50%' },
'100%': { backgroundPosition: '0% 50%' },

I want an animation like:

pink -> purple -> blue -> purple -> pink

but I want the transition to always go from left to right. Currently it goes left to right but goes from right to left at the end, and repeats.

I have the following classes using tailwind 3.4.17:

bg-gradient-to-r from-pink-500 via-purple-500 to-blue-500 bg-[length:400%_100%] animate-[gradientMove_2s_linear_infinite]

with keyframes:

'0%': { backgroundPosition: '0% 50%' },
'50%': { backgroundPosition: '100% 50%' },
'100%': { backgroundPosition: '0% 50%' },

I want an animation like:

pink -> purple -> blue -> purple -> pink

but I want the transition to always go from left to right. Currently it goes left to right but goes from right to left at the end, and repeats.

Share Improve this question edited Mar 21 at 16:54 renatodamas asked Mar 21 at 15:40 renatodamasrenatodamas 19.7k8 gold badges41 silver badges59 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can just create a gradient of

pink -> purple -> blue -> purple -> pink

like so:

bg-linear-[to_right,pink_10%,purple_30%,blue_50%,purple_70%,pink_90%]

and then animate it left to right with keyframes:

@keyframes gradientMove {
  0% {
    background-position-x: left;
  }
  100% {
    background-position-x: right;
  }
}

Having animate-[gradientMove_2s_linear_infinite] causes it to repeat endlessly, if you want it to stop after the animation just replace with animate-[gradientMove_2s_linear_forwards]. This sets animation-fill-mode.

All together:

bg-linear-[to_right,pink_10%,purple_30%,blue_50%,purple_70%,pink_90%] bg-[length:400%_100%] animate-[gradientMove_2s_linear_forwards]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论