frankV
frankV

Reputation: 5513

Simple jQuery Animation Replace Text from Array

I want to replace the text in a 'div span' with text from an array. I'd like this simple animation to do two things...

  1. Fly or fade the text in randomly
  2. Loop continously

I have a fiddle setup where I've been trying different things; http://jsfiddle.net/fmdfrank/W47QV/

Anybody?

Upvotes: 3

Views: 5144

Answers (1)

Michael Sparks
Michael Sparks

Reputation: 645

use jquery .queue & .dequeue to store each operation in the fx queue. this will let the text change behave as part of the animation queue. then check if you need to loop.

in a fiddle: http://jsfiddle.net/W47QV/4/

$(document).ready(function() {

    var items = ["Two", "Three", "Four", "Five", "Six", "One"],
        $text = $( '#div1 span' ),
        delay = 2; //seconds

    function loop ( delay ) {
        $.each( items, function ( i, elm ){
            $text.delay(delay*1E3).fadeOut();
            $text.queue(function(){
                $text.html( items[i] );
                $text.dequeue();
            });
            $text.fadeIn();
            $text.queue(function(){
                if(i == items.length-1){
                    loop(delay);   
                }
                $text.dequeue();
            });
        });
    }

    loop(delay);

});​

Upvotes: 5

Related Questions