Todd Todd - 1 year ago 204
CSS Question

Cycling animation of rainbow-coloured text


I'm trying to take a block of text, color each letter according to calculated position between HSL 0deg and 360deg, and on hover animate the colors to the right. I know this is weird, but bear with me. What I want is animated rainbow text on hover.

I've covered the logic of making all of this happen once, but can't get hover cycling behavior to work.

here is a link to the

I've tried using JavaScript mouse events and jQuery's .hover(). My initial thought was to set an interval on mouse enter and clear it on exit.

I'd truly appreciate any help on this obviously very important project.

Answer Source

You might wanna think about how this'll affect UX, but what about this:

Ok, so rotating through all the colours of the rainbow is easy enough using CSS-animations. The issue is then linking them up to all your span tags such that the animation starts in the right place. (i.e. you need the green letter to start it's animation from the colour green etc.) To do this, we can use animation-delay:

which we can use to start the rainbow animation at the appropriate colour for each letter. By using a linear timing function, it's simple to determine at what time the animation will arrive at each colour. Therefore, it's just a matter of attaching the right animation-delay value to each <span> element. I do that by just taking your already generated HTML and adding in the CSS rules to each element's style attribute:

var animTime = 6, // time for the animation in seconds
    hueChange = 3, // the hue change from one span element to the next
    prefixes = ["", "-webkit-", "-moz-", "-o-"],
    numPrefixes = prefixes.length;

$('.unicorn').find('span').each(function (i) {
    for (var j = 0; j < numPrefixes; j++) {
        $(this).css(prefixes[j] + 'animation-delay', (animTime * ((i * hueChange) % 360) / 360) - animTime + 's');

but you could do this at the same time as you're generating all your span elements. Then it's just a case of setting up the animation using CSS:

.unicorn:hover span {

    animation: colorRotate 6s linear 0s infinite;


@keyframes colorRotate {
    from {
        color: rgb(255, 0, 0);
    16.6% {
        color: rgb(255, 0, 255);
    33.3% {
        color: rgb(0, 0, 255);
    50% {
        color: rgb(0, 255, 255);
    66.6% {
        color: rgb(0, 255, 0);
    83.3% {
        color: rgb(255, 255, 0);
    to {
        color: rgb(255, 0, 0);

All this gets us to here:

Now, if you don't want the colours to reset once someone is no longer hovering over .unicorn then you can use animation-play-state:

However, I was finding that Chrome has an issue with combining an initial value of -webkit-animation-play-state:paused; and a negative value of -webkit-animation-delay such that it just displayed the first frame (i.e. color: rgb(255,0,0); in this case). Therefore I had to use an event listener to add a class containing the animation CSS on the first hover, and this leads us to:

(that bug in chrome can be tracked here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download