1

i want to animate a string of text taken from an html element using jquery:

<h1>Animate</h1>

for the jquery part:

$(document).ready(function() {

$( "h1" ).animate({ fontSize: "90px" }, 1500 )
     .animate({ fontSize: "50px" }, 1500 );

});

this animates the whole h1 text value, however i want to animate each character in the h1 text.

the second jquery part:

$(document).ready(function () {

    var animateChar = $("h1").text();
    for(i=0; i<animateChar.length; i++) {

        //alert(i + ': ' + animateChar.charAt(i));
        // for every animateChar.charAt[i] want to run this
        //jquery animation.
        //$( "h1" ).animate({ fontSize: "90px" }, 1500 )
        //.animate({ fontSize: "50px" }, 1500 );

        }
 });

this is the bit where im stuck. thanks

3
  • 1
    you'll have to wrap each character in a span - text nodes are indivisible. Commented Jul 23, 2012 at 15:07
  • 1
    I'm not sure that's possible without separating out every character in to another container such as a <span>. I guess you could do that first, then loop through them. Commented Jul 23, 2012 at 15:07
  • yeh thats interesting idea!! let me try it out!! thanks Commented Jul 23, 2012 at 15:09

3 Answers 3

6

You can use jQuery functions on DOM elements. Not on characters apart. You should use different DOM elements for each character:

<span>A</span><span>N</span><span>I</span>...

with something like this must do the trick

$('span').each(function() {
    var that = $(this);
    setTimeout(function() { 
        that.animate({ fontSize: "90px" }, 1500 )
            .animate({ fontSize: "50px" }, 1500 );
    },that.index()*100);
});​

-edit-

working jSFIDDLE

-edit 2-

without messy HTML JSFIDDLE(well it's still messy, but javascript makes it messy ;) )

Sign up to request clarification or add additional context in comments.

2 Comments

I agree. But don't flood your html code with such unsemantical stuff. Use lettering.js tool.
@Marakoss True. Made an edit (still making my HTML messy on clientside) with readable template html
0

If you want to keep your HTML clean, I'd add the following method to wrap your characters in span tags. Execute this onLoad using wrapCharacters($("h1")) before you animate anything, then animate characters returned by $(".animate").

function wrapCharacters(obj)
{
    var html = '';
    var text = obj.text();
    for (var i = 0; i < text.length; i++)
    {
        html += '<span class="animate">' + text[i] + '</span>';
    }
    obj.html(html);
}

2 Comments

can you clarify using this more!!
At the start of the function run onLoad execute this on any of the elements you want to animate like this (wrapCharacters($("h1")) is fine if you only have one h1 tag, otherwise loop them and do wrapCharacters on each one - you can use $.each for this), it should wrap all characters in a container with a span of the appropriate class, then use rsplak's answer on how to animate those spans.
0
<span class="animate">A</span><span class="animate">n</span><span class="animate">i</span><span class="animate">m</span><span class="animate">a</span><span class="animate">t</span><span class="animate">e</span>

$(document).ready(function() {

   var count = 0;       

   function animation(elm) {
       if(count == $(".animate").length)
       {
          clearInterval(id);
          return;
       }

       $( elm ).animate({ fontSize: "90px" }, 1500 )
     .animate({ fontSize: "50px" }, 1500 );
     count++;
   }

   var id = setInterval(animation($(".animate")[count]), 3200);//Give time for animation to run.

});

This will animate each character.

1 Comment

this deosnt animate letters, it animates all of them at once! :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.