Owen Melbourne Owen Melbourne - 9 months ago 68
jQuery Question

jQuery text colour change animation

I've been rattling my head today to try and get a mouseover text colour changing script, that animates from changing the first letter of the string then each letter after that.

So effectively it creates almost a colour slide in change? I hope you understand what i mean.

I got really confused and ended up getting the text i wanted animated into a string, converting the string to an array then using a loop with set timeout to change each element in the array to the new color.

but my logic is all over the place im sure and things just are not working!!

could somebody please give this a look over or provide me with a solution! thanks


Answer Source

How about something like this: jsFiddle example.


var string = $('h2').text();
var letters = string.split('');
var x = string.length;
$.each(letters, function(index) {
    $('h2').append('<span id="e' + index + '" class="normal">' + letters[index] + '</span>');

function Animate(elem) {
    setTimeout(function() {
            'color': 'yellow'
        }, 500, function(){elem++;Animate(elem)});
    }, 50);
$('h2').mouseenter(function() {
}).mouseleave(function() {});