Guest User

Untitled

a guest
Sep 11th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. functions fade() and animateFade() from http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation */
  2.  
  3. var TimeToFade = 1000.0; //time to fade in and fade out
  4. var timeToShow = 6000.0; //how long to display each piece of content
  5. var timeToRest = 250; //how long to wait before displaying next piece of content
  6. var currentContentItem = 0; //which content item you wish to start with, zero based
  7. var contentContainer; //the containing div
  8. var contentItems; //all the content items
  9. var fadeOutTimer; //timer for handling fade outs
  10. var fadeInTimer; //timer for handling fade ins
  11.  
  12. function CycleQuotes()
  13. {
  14. //get all the divs inside the contentContainer div. Each of these is a unique quote
  15. contentContainer = document.getElementById("contentContainer");
  16. contentItems = contentContainer.getElementsByTagName("div");
  17. contentItems[0].style.display = "block"; //all child divs are currently hidden so show the first one
  18. setTimeout("FadeOut()", timeToShow);
  19. }
  20.  
  21. function PrevClick()
  22. {
  23. PausePlayClick(false);
  24.  
  25. //make sure the current quote is displayed
  26. contentItems[currentContentItem].style.display = "none";
  27.  
  28. //change the current quote
  29. if (currentContentItem == 0)
  30. { currentContentItem = contentItems.length - 1; }
  31. else
  32. { currentContentItem--; }
  33.  
  34. contentItems[currentContentItem].style.display = "block";
  35. }
  36.  
  37. function NextClick()
  38. {
  39. PausePlayClick(false);
  40.  
  41. //make sure the current quote is displayed
  42. contentItems[currentContentItem].style.display = "none";
  43.  
  44. //change the current quote
  45. if (currentContentItem == contentItems.length - 1) { currentContentItem = 0; }
  46. else { currentContentItem++; }
  47. contentItems[currentContentItem].style.display = "block";
  48. }
  49.  
  50. function PausePlayClick(playing)
  51. {
  52. //get the play/pause buttons
  53. var playbutton = document.getElementById('playimg');
  54. var pausebutton = document.getElementById('pauseimg');
  55.  
  56. //start/stop the current fade effects
  57. if (playing)
  58. {
  59. //change play icon to pause
  60. pausebutton.style.display = "";
  61. playbutton.style.display = "none";
  62.  
  63. setTimeout("FadeOut()", 0);
  64. }
  65. else
  66. {
  67. //make sure we have a quote showing
  68. if (contentContainer.FadeState < 0) { fade(contentContainer.id); }
  69.  
  70. //change pause icon to play icon
  71. pausebutton.style.display = "none";
  72. playbutton.style.display = "";
  73.  
  74. //stop timers
  75. clearTimeout(fadeOutTimer);
  76. clearTimeout(fadeInTimer);
  77. }
  78. }
  79.  
  80. //Fade out quote
  81. function FadeOut()
  82. {
  83. fade(contentContainer.id);
  84. fadeOutTimer = setTimeout("FadeIn()", TimeToFade + timeToRest);
  85. }
  86.  
  87. //Fade new quote in
  88. function FadeIn()
  89. {
  90. //hide the current quote (the container div will already have opacity set to 100% so this quote is currently invisible)
  91. contentItems[currentContentItem].style.display = "none";
  92.  
  93. //show the next quote
  94. if (currentContentItem == contentItems.length - 1) { currentContentItem = 0; }
  95. else { currentContentItem++; }
  96. contentItems[currentContentItem].style.display = "block";
  97.  
  98. //fade in the container
  99. fade(contentContainer.id);
  100.  
  101. //recurse
  102. fadeInTimer = setTimeout("FadeOut()", timeToShow);
  103. }
  104.  
  105. function fade(eid) //function from http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation
  106. {
  107. var element = document.getElementById(eid);
  108. if(element == null)
  109. return;
  110.  
  111. if(element.FadeState == null)
  112. {
  113. if(element.style.opacity == null
  114. || element.style.opacity == ''
  115. || element.style.opacity == '1')
  116. {
  117. element.FadeState = 2;
  118. }
  119. else
  120. {
  121. element.FadeState = -2;
  122. }
  123. }
  124.  
  125. if(element.FadeState == 1 || element.FadeState == -1)
  126. {
  127. element.FadeState = element.FadeState == 1 ? -1 : 1;
  128. element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  129. }
  130. else
  131. {
  132. element.FadeState = element.FadeState == 2 ? -1 : 1;
  133. element.FadeTimeLeft = TimeToFade;
  134. setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  135. }
  136.  
  137. }
  138.  
  139. function animateFade(lastTick, eid) //function from http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation
  140. {
  141. var curTick = new Date().getTime();
  142. var elapsedTicks = curTick - lastTick;
  143.  
  144. var element = document.getElementById(eid);
  145.  
  146. if(element.FadeTimeLeft <= elapsedTicks)
  147. {
  148. element.style.opacity = element.FadeState == 1 ? '1' : '0';
  149. element.style.filter = 'alpha(opacity = '
  150. + (element.FadeState == 1 ? '100' : '0') + ')';
  151. element.FadeState = element.FadeState == 1 ? 2 : -2;
  152. return;
  153. }
  154.  
  155. element.FadeTimeLeft -= elapsedTicks;
  156. var newOpVal = element.FadeTimeLeft/TimeToFade;
  157. if(element.FadeState == 1)
  158. newOpVal = 1 - newOpVal;
  159.  
  160. element.style.opacity = newOpVal;
  161. element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  162.  
  163. setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment