marquee.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /************各种滚动或跑马灯特效****************/
  2. /************以下是文字上下翻屏滚动特效****************/
  3. /*调用方法
  4. new Dron_ScrollBox("id").init();
  5. */
  6. function Dron_ScrollBox(uid){
  7. this.scrollBox = document.getElementById(uid);
  8. this.scrollBoxHeight = this.scrollBox.clientHeight;
  9. this.scrollBoxInner = this.scrollBox.innerHTML;
  10. this.scrollCol = this.scrolln = 0;
  11. this.setScroll = function (){
  12. this.scrollBox.scrollTop = this.scrollCol + this.scrolln;
  13. if(this.scrolln==this.scrollBoxHeight){
  14. return this.addScroll()
  15. }else{
  16. this.scrolln ++;
  17. }
  18. var o = this;
  19. function m(){o.setScroll();}
  20. setTimeout(m,20);
  21. }
  22. this.addScroll = function (){
  23. this.scrollBox.innerHTML += this.scrollBoxInner;
  24. this.scrollCol = this.scrollBox.scrollTop;
  25. this.scrolln = 0;
  26. var o = this;
  27. function m(){o.setScroll();}
  28. setTimeout(m,3000);
  29. }
  30. this.init = this.addScroll;
  31. }
  32. /***********图片或文字滚动特效***************/
  33. /*调用方法
  34. Marquee.newInstance("Left", "partners").Init();
  35. */
  36. String.prototype.format = function(){
  37. var tmpStr = this;
  38. var iLen = arguments.length;
  39. for(var i=0;i<iLen;i++){
  40. tmpStr = tmpStr.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
  41. }
  42. return tmpStr;
  43. }
  44. function $() {
  45. var elements = new Array();
  46. for (var i = 0; i < arguments.length; i++) {
  47. var element = arguments[i];
  48. if (typeof element == 'string')
  49. element = document.getElementById(element);
  50. if (arguments.length == 1)
  51. return element;
  52. elements.push(element);
  53. }
  54. return elements;
  55. }
  56. function IMarquee(){
  57. function throwError(){alert("接口未实现:" + arguments[0]);}
  58. this.Scroll = function(){throwError("Scroll");}
  59. this.Clone = function(){throwError("Clone");}
  60. }
  61. function AbstractMarquee(){
  62. IMarquee.apply(this);
  63. var ref = this;
  64. var timer = null;
  65. var container = null;
  66. var indexs = ["ContainerID", "Delay", "Amount", "Width", "Height"];
  67. this.Amount = 1;
  68. this.Delay = 30;
  69. this.Width = 0;
  70. this.Height = 0;
  71. this.ContainerID = "";
  72. this.Start = function(){
  73. clearTimer();
  74. timer = setInterval(ref.Scroll, ref.Delay);
  75. }
  76. this.Stop = function(){
  77. clearTimer();
  78. }
  79. this.Pause = function(){
  80. clearTimer();
  81. }
  82. this.Init = function(){
  83. container = $(this.ContainerID);
  84. if(container == null) {alert("无法找到id为{0}的对象,初始化失败。".format(this.ContainerID));return;};
  85. container.style.overflow = "hidden";
  86. if(this.Width > 0) container.style.width = this.Width + "px";
  87. if(this.Height > 0) container.style.height = this.Height + "px";
  88. this.Clone();
  89. this.AttachEvent();
  90. this.Start();
  91. }
  92. this.AttachEvent = function(){
  93. container.onmouseover = ref.Pause;
  94. container.onmouseout = ref.Start;
  95. }
  96. function clearTimer(){
  97. if(timer != null)clearInterval(timer);
  98. }
  99. function _Marquee(){
  100. var max = Math.min(indexs.length, arguments.length);
  101. for(var i=0;i<max;i++)
  102. this[indexs[i]] = arguments[i];
  103. }
  104. _Marquee.apply(this, arguments);
  105. }
  106. function MarqueeUp(){
  107. AbstractMarquee.apply(this, arguments);
  108. var ref = this;
  109. var container = $(this.ContainerID);
  110. this.Clone = function(){
  111. container.innerHTML = '<div>{0}</div><div>{0}</div>'.format(container.innerHTML);
  112. }
  113. this.Scroll = function(){
  114. with(container){
  115. if(scrollTop >= lastChild.offsetTop) scrollTop -= firstChild.offsetHeight;
  116. else scrollTop += ref.Amount;
  117. }
  118. }
  119. }
  120. function MarqueeDown(){
  121. AbstractMarquee.apply(this, arguments);
  122. var ref = this;
  123. var container = $(this.ContainerID);
  124. this.Clone = function(){
  125. container.innerHTML = '<div>{0}</div><div>{0}</div>'.format(container.innerHTML);
  126. container.scrollTop = container.scrollHeight;
  127. }
  128. this.Scroll = function(){
  129. with(container){
  130. if(scrollTop <= firstChild.offsetTop) scrollTop += lastChild.offsetHeight;
  131. else scrollTop -= ref.Amount;
  132. }
  133. }
  134. }
  135. function MarqueeLeft(){
  136. AbstractMarquee.apply(this, arguments);
  137. var ref = this;
  138. var container = $(this.ContainerID);
  139. this.Clone = function(){
  140. container.innerHTML = '<table cellspacing="0" cellpadding="0" border="0"><tr><td>{0}</td><td>{0}</td></tr></table>'.format(container.innerHTML);
  141. }
  142. this.Scroll = function(){
  143. with(container){
  144. if(scrollLeft >= firstChild.rows[0].cells[1].offsetLeft) scrollLeft -= firstChild.rows[0].cells[0].offsetWidth;
  145. else scrollLeft += ref.Amount;
  146. }
  147. }
  148. }
  149. function MarqueeRight(){
  150. AbstractMarquee.apply(this, arguments);
  151. var ref = this;
  152. var container = $(this.ContainerID);
  153. this.Clone = function(){
  154. container.innerHTML = '<table cellspacing="0" cellpadding="0" border="0"><tr><td>{0}</td><td>{0}</td></tr></table>'.format(container.innerHTML);
  155. container.scrollLeft = container.scrollWidth;
  156. }
  157. this.Scroll = function(){
  158. with(container){
  159. if(scrollLeft <= firstChild.rows[0].cells[0].offsetLeft) scrollLeft += firstChild.rows[0].cells[1].offsetWidth;
  160. else scrollLeft -= ref.Amount;
  161. }
  162. }
  163. }
  164. var Marquee = {
  165. Type : {
  166. UP : function(){return new MarqueeUp(arguments[0]);},
  167. DOWN : function(){return new MarqueeDown(arguments[0]);},
  168. LEFT : function(){return new MarqueeLeft(arguments[0]);},
  169. RIGHT : function(){return new MarqueeRight(arguments[0]);}
  170. },
  171. newInstance : function(type, container){
  172. return this.Type[type.toUpperCase()].call(this, container);
  173. }
  174. }