用 Javascript 制作可暂停的滚动公告板

作者: Dynamic Drive     来源: DynamicDrive.com     时间: 2006-07-14 11:44:19
摘要: 这是转载国外的 Javascript 可暂停滚动公告板代码,可定制性非常强,滚动时间可以任意设置,显示样式由CSS控制,可以很容易的整合在您自己的网站上。对于懂PHP的很容易做一个动态的新闻公告板及其他应用。
归类: Javascript,

关键词: 可暂停, 滚动, 公告板, Javascript, js特效,

由标题很容易看出来,这是一个可以在显示每一个消息时都会暂停的动态滚动条,滚动条的样式完全由CSS控制,当然也包括显示尺寸。调整尺寸后就可以很容易的制作出一个单行的滚动公告板。

-------------------------------------------
点击这里查看示例
-------------------------------------------


代码:

将以下代码放在网页的<head></head>之间。

CSS:
  1.  
  2. <style type="text/css">
  3.  
  4. /*Example CSS for the two demo scrollers*/
  5.  
  6. #pscroller1{
  7. width: 200px;
  8. height: 100px;
  9. border: 1px solid black;
  10. padding: 5px;
  11. background-color: lightyellow;
  12. }
  13.  
  14. #pscroller2{
  15. width: 450px;
  16. height: 22px;
  17. border: 1px solid black;
  18. padding: 3px;
  19. }
  20.  
  21. #pscroller2 a{
  22. text-decoration: none;
  23. }
  24.  
  25. .someclass{ //class to apply to your scroller(s) if desired
  26. }
  27.  
  28. BODY, td, div {
  29. FONT-SIZE: 12px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
  30. }
  31. A {
  32. COLOR: #003499; TEXT-DECORATION: none; FONT-SIZE: 12px;
  33. }
  34. A:hover {
  35. COLOR: #000000; TEXT-DECORATION: underline; FONT-SIZE: 12px;
  36. }
  37.  
  38. </style>
  39.  
  40.  


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


然后将下面的代码插入到网页的<body></body>之间,可以放在你想显示的任意位置。

Javascript:
  1.  
  2. <script type="text/javascript">
  3.  
  4. //new pausescroller(name_of_message_array, CSS_ID, CSS_classname, pause_in_miliseconds)
  5.  
  6. new pausescroller(pausecontent, "pscroller1", "someclass", 3000)
  7. document.write("<br />")
  8. new pausescroller(pausecontent2, "pscroller2", "someclass", 2000)
  9.  
  10. </script>
  11.  



调用说明:
Code:

new pausescroller(pausecontent, "pscroller1", "someclass", 3000)



其中 pausecontent 是你在<head></head>之间的代码定义的显示内容。pscroller1 是你在CSS中定义的唯一的滚动层ID, someclass 是任意您想附加的显示样式, 3000 是控制暂停的时间,以毫秒为单位。

控制显示样式的方法就是用标准的CSS语法:
CSS:
  1.  
  2. #pscroller1{
  3. width: 200px;
  4. height: 100px;
  5. border: 1px solid black;
  6. padding: 5px;
  7. background-color: lightyellow;
  8. }
  9.  


如果想创建单行的滚动条可以简单的将 height 值改为 20px 或是 25px 即可。

可暂停的滚动公告板,就这样完成了。



推荐链接:(联系 QQ :326801485)