Scripts and functions that I have written or found useful

Dynamic Date ticker/scroller

September 8th, 2006 Posted in Javascript, MYSQL

This is useful for displaying dynamic data in a small space, it scrolls the data and pauses at every line. It will also pause on mouseover

JAVASCRIPT:
  1. <script type="text/javascript"><!--
  2.  
  3.  
  4. /*Example message arrays for the two demo scrollers*/
  5. var pausecontent2=new Array()
  6. pausecontent2[0]='First item'pausecontent2[1]='Second item'pausecontent2[2]='Third item'// --></script>
  7.  
  8. <script type="text/javascript"><!--
  9.  
  10.  
  11. /***********************************************
  12. * Pausing up-down scroller- © Dynamic Drive (<a href="http://www.dynamicdrive.com/" mce_href="http://www.dynamicdrive.com/">www.dynamicdrive.com</a>)
  13. * This notice MUST stay intact for legal use
  14. * Visit <a href="http://www.dynamicdrive.com/" mce_href="http://www.dynamicdrive.com/">http://www.dynamicdrive.com/</a> for this script and 100s more.
  15. ***********************************************/
  16.  
  17. function pausescroller(content, divId, divClass, delay){
  18. this.content=content //message array content
  19. this.tickerid=divId //ID of ticker div to display information
  20. this.delay=delay //Delay between msg change, in miliseconds.
  21. this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
  22. this.hiddendivpointer=1 //index of message array for hidden div
  23. document.write('
  24. <div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden" mce_style="position: relative; overflow: hidden">
  25. <div class="innerDiv" style="position: absolute; width: 100%" mce_style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div>
  26. <div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" mce_style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div>
  27. </div>
  28. ')
  29. var scrollerinstance=this
  30. if (window.addEventListener) //run onload in DOM2 browsers
  31. window.addEventListener("load", function(){scrollerinstance.initialize()}, false)
  32. else if (window.attachEvent) //run onload in IE5.5+
  33. window.attachEvent("onload", function(){scrollerinstance.initialize()})
  34. else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
  35. setTimeout(function(){scrollerinstance.initialize()}, 500)
  36. }
  37.  
  38. //                                  -
  39. // initialize()- Initialize scroller method.
  40. // -Get div objects, set initial positions, start up down animation
  41. //                                  -
  42.  
  43. pausescroller.prototype.initialize=function(){
  44. this.tickerdiv=document.getElementById(this.tickerid)
  45. this.visiblediv=document.getElementById(this.tickerid+"1")
  46. this.hiddendiv=document.getElementById(this.tickerid+"2")
  47. this.visibledivtop=parseInt(pausescroller.getCSSpadding(this.tickerdiv))
  48. //set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
  49. this.visiblediv.style.width=this.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px"
  50. this.getinline(this.visiblediv, this.hiddendiv)
  51. this.hiddendiv.style.visibility="visible"
  52. var scrollerinstance=this
  53. document.getElementById(this.tickerid).onmouseover=function(){scrollerinstance.mouseoverBol=1}
  54. document.getElementById(this.tickerid).onmouseout=function(){scrollerinstance.mouseoverBol=0}
  55. if (window.attachEvent) //Clean up loose references in IE
  56. window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null})
  57. setTimeout(function(){scrollerinstance.animateup()}, this.delay)
  58. }
  59. //                                  -
  60. // animateup()- Move the two inner divs of the scroller up and in sync
  61. //                                  -
  62.  
  63. pausescroller.prototype.animateup=function(){
  64. var scrollerinstance=this
  65. if (parseInt(this.hiddendiv.style.top)>(this.visibledivtop+5)){
  66. this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-5+"px"
  67. this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-5+"px"
  68. setTimeout(function(){scrollerinstance.animateup()}, 50)
  69. }
  70. else{
  71. this.getinline(this.hiddendiv, this.visiblediv)
  72. this.swapdivs()
  73. setTimeout(function(){scrollerinstance.setmessage()}, this.delay)
  74. }
  75. }
  76.  
  77. //                                  -
  78. // swapdivs()- Swap between which is the visible and which is the hidden div
  79. //                                  -
  80.  
  81. pausescroller.prototype.swapdivs=function(){
  82. var tempcontainer=this.visiblediv
  83. this.visiblediv=this.hiddendiv
  84. this.hiddendiv=tempcontainer
  85. }
  86.  
  87. pausescroller.prototype.getinline=function(div1, div2){
  88. div1.style.top=this.visibledivtop+"px"
  89. div2.style.top=Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)+"px"
  90. }
  91.  
  92. //                                  -
  93. // setmessage()- Populate the hidden div with the next message before it's visible
  94. //                                  -
  95.  
  96. pausescroller.prototype.setmessage=function(){
  97. var scrollerinstance=this
  98. if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)
  99. setTimeout(function(){scrollerinstance.setmessage()}, 100)
  100. else{
  101. var i=this.hiddendivpointer
  102. var ceiling=this.content.length
  103. this.hiddendivpointer=(i+1>ceiling-1)? 0 : i+1
  104. this.hiddendiv.innerHTML=this.content[this.hiddendivpointer]
  105. this.animateup()
  106. }
  107. }
  108.  
  109. pausescroller.getCSSpadding=function(tickerobj){ //get CSS padding value, if any
  110. if (tickerobj.currentStyle)
  111. return tickerobj.currentStyle["paddingTop"]
  112. else if (window.getComputedStyle) //if DOM2
  113. return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
  114. else
  115. return 0
  116. }
  117.  
  118. // --></script>
  119.  
  120. <script type="text/javascript"><!--
  121.  
  122.  
  123. //new pausescroller(name_of_message_array, CSS_ID, CSS_classname, pause_in_miliseconds)
  124.  
  125. new pausescroller(pausecontent2, "pscroller2", "someclass", 2000)
  126.  
  127. // --></script>

Post a Comment