Animated Pitchbars

/ Published in: jQuery
Save to your folder(s)

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.

Copy this code and paste it in your HTML
  1. // The javascript (the important part)
  2. $(document).ready(function(){
  3. pitcher(1,1);
  4. });
  5. function pitcher(num,pdl){
  6. var pitches=3; var shortdelay=500; var longdelay=5000; // edit these
  7. var n1=0; var n2=0; var n3=0; var out=''; var outdel=0; var indel=0;
  8. while(n1<=$("#pitches > li:nth-child("+num+")").html().length){
  9. out=out+'<span style="opacity:0;">'+$("#pitches > li:nth-child("+num+")").html().substr(n1,1)+'</span>';
  10. n1++;
  11. }
  12. $('div.activepitch'+pdl+' > span').each(function(){
  13. outdel+=15;
  14. $(this).delay(outdel).animate({
  15. opacity: .0
  16. },500,function(){
  17. n2++;
  18. if(n2==$('div.activepitch'+pdl+' > span').size()){
  19. $('div.activepitch'+pdl).html(out);
  20. $('div.activepitch'+pdl+' > span').each(function(){
  21. indel+=15;
  22. $(this).delay(indel).animate({
  23. opacity:1
  24. },500,function(){
  25. n3++;
  26. if(n3==$('div.activepitch'+pdl+' > span').size()){
  27. if(pdl==pitches){
  28. pdl=1;
  29. }else{
  30. pdl++;
  31. }
  32. if(num==$('#pitches > li').size()){
  33. num=1;
  34. }else{
  35. num++;
  36. }
  37. if(pdl==1){
  38. setTimeout(function(){pitcher(num,pdl)},longdelay);
  39. }else{
  40. setTimeout(function(){pitcher(num,pdl)},shortdelay);
  41. }
  42. }
  43. });
  44. })
  45. }
  46. });
  47. });
  48. }
  52. // The HTML (also the important part)
  53. <ul id="pitches">
  54. <li>Pitch 1</li>
  55. <li>Pitch 2</li>
  56. <li>Pitch 3</li>
  57. <li>Pitch 4</li>
  58. <li>Pitch 5</li>
  59. </ul>
  60. <div class="pitchbar activepitch1"><span></span></div>
  61. <div class="pitchbar activepitch2"><span></span></div>
  62. <div class="pitchbar activepitch3"><span></span></div>


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.