SEE ANOTHER ANSWER COVERING A SINGLE QUESTION
BON! No need for setInterval when you are dealing with .animate()
Even better approach without first break / jump change and with dynamically calculated span width:
Live demo
CSS
h1 span{ vertical-align:top; }
JQ:
$(function() { var t=["changing", "dynamic", "cool"], $h1 = $("#changingtext"), $sp = $h1.find("span"), i=0, widths=[]; $.each(t, function(i, v){ var el = $('<span />', {text:v}).appendTo($h1); widths.push(el.width()); el.remove(); }); $sp.css({opacity:0}); (function loop(){ i = ++i%t.length; $sp.text(t[i]).animate({width:widths[i]}, 1000, function(){ $(this).animate({opacity:1},1000).delay(3000).animate({opacity: 0}, 1000, loop); }); })(); });
This code group
$.each(t, function(i, v){ var el = $('<span />', {text:v}).appendTo($h1); widths.push(el.width()); el.remove(); });
stores in our array widths[] all the necessary widths that we need.
We will consider iterations of widths[] just as you already did for t[]
than we create a recursive self-invoking function (no need for setInterval !), where we check for i to become 0 with a simple Modulo Operator (Reminder) %
i = ++i%t.length;
The looping function is simply achieved by referring to this function inside the last chain callback.
source share