Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 984
frankV Avatar asked Mar 01 '26 13:03

frankV


1 Answers

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);

});​
like image 119
Michael Sparks Avatar answered Mar 03 '26 04:03

Michael Sparks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!