Return to Snippet

Revision: 46996
at May 29, 2011 05:28 by FatFolderDesigner


Initial Code
// The javascript (the important part)
    $(document).ready(function(){
        pitcher(1,1);
    });
    function pitcher(num,pdl){
        var pitches=3; var shortdelay=500; var longdelay=5000; // edit these
        var n1=0; var n2=0; var n3=0; var out=''; var outdel=0; var indel=0;
        while(n1<=$("#pitches > li:nth-child("+num+")").html().length){
            out=out+'<span style="opacity:0;">'+$("#pitches > li:nth-child("+num+")").html().substr(n1,1)+'</span>';
            n1++;
        }
        $('div.activepitch'+pdl+' > span').each(function(){
           outdel+=15;
            $(this).delay(outdel).animate({
                opacity: .0
            },500,function(){
                n2++;
                if(n2==$('div.activepitch'+pdl+' > span').size()){
                    $('div.activepitch'+pdl).html(out);
                    $('div.activepitch'+pdl+' > span').each(function(){
                        indel+=15;
                        $(this).delay(indel).animate({
                            opacity:1
                        },500,function(){
                            n3++;
                            if(n3==$('div.activepitch'+pdl+' > span').size()){
                                if(pdl==pitches){
                                    pdl=1;
                                }else{
                                    pdl++;
                                }
                                if(num==$('#pitches > li').size()){
                                    num=1;
                                }else{
                                    num++;
                                }
                                if(pdl==1){
                                    setTimeout(function(){pitcher(num,pdl)},longdelay);
                                }else{
                                    setTimeout(function(){pitcher(num,pdl)},shortdelay);
                                }
                            }
                        });
                    })
                }
            });
        });
    }



// The HTML (also the important part)
<ul id="pitches">
	<li>Pitch 1</li>
	<li>Pitch 2</li>
	<li>Pitch 3</li>
	<li>Pitch 4</li>
	<li>Pitch 5</li>
</ul>
<div class="pitchbar activepitch1"><span></span></div>
<div class="pitchbar activepitch2"><span></span></div>
<div class="pitchbar activepitch3"><span></span></div>

Initial URL
http://fatfolderdesign.com/255/css/animated-pitchbars

Initial Description
The comments for this are broken down into 2 parts, first the script is talked about, then theres a little about the HTML code that the script relies on. You can find more information at the URL, as well as an example of it in action. You can leave comments here or at the link if you have any questions or comments, or if you found a nice use for it.

First the script.
Use jquery to load this once the document is complete. The function starts with variables, the first line of them are ones for easy modification, pitches in the number of pitches it will show at once, and the shortdelay/longdelay is the delay between animating individual and pitches and the delay between animating the last displayed pitch and starting from the top again respectively.

After that there is a while statement, this takes the appropriate pitch and wraps each element around a span for animation purposes. Then it gets into the main scripting and animation section.

The each function runs through each of the spans and starts the animation on it. First is fades the current value out, it inserts the new pitch, pre-set at 0 opacity, and fades it in span-by-span. Once the loops is complete it runs a couple checks; is it the last pitch being displayed, is it the last pitch in the list of all pitches, and what it’s going to be doing next. In the first two cases it resets the appropriate counter, the last check determines if it will run the short delay (between pitches being displayed) or the long delay (between displaying the last pitch and starting the animation cycle again).

With this setup the script will run forever, looping when needed. It does require some HTML formatting though, is in the second section.

Second, the HTML
The first section is the list of pitches, stored in an un-ordered list with the id "pitches". Each pitch simply goes in a list item. Theres nothing more to creating the list of all your pitches. 

The display goes inside the divs. The class <code>pitchbar</code> is my own simply for formattingand is not required the other one, <code>activepitch#</code> is the area where the the current pitches will be displayed. The "#" is for each pitch that you display at a time from 1 to your max, for example the code below has a <code>pitches</code> of three, so I have 3 divs; <code>activepitch1</code>, <code>activepitch2</code>, <code>activepitch3</code>. The example at the url has 5 divs and a <code>pitches</code> value of 5 as well.

Initial Title
Animated Pitchbars

Initial Tags
css, data, html, jquery

Initial Language
jQuery