weescroller.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var weescroller = function(idUl, options) {
  2. var self = this;
  3. this.pausetimer = null;
  4. this.steptimer = null;
  5. this.oUl = $(idUl);
  6. this.oOUl = this.oUl.clone();
  7. this.CTL = 0;
  8. //设置默认属性
  9. this.options = { //默认值
  10. row: 5, //显示行数
  11. time: 15, //速度(越大越慢)
  12. pause: 2000, //停顿时间
  13. direct: 0, //0向上,1向下,2向左,3向右
  14. };
  15. this.options = $.extend(self.options, options || {});
  16. //向下
  17. this.Next = function() {
  18. if (!self.canscroll) return false;
  19. clearTimeout(self.pausetimer);
  20. clearInterval(self.steptimer);
  21. var height = self.oUl.find("li:first").height();
  22. self.steptimer = setInterval(function(){
  23. if (self.CTL >= height) {
  24. self.CTL = 0;
  25. clearInterval(self.steptimer);
  26. self.oUl.find("li:first").appendTo(self.oUl);
  27. self.oUl.get(0).scrollTop = 0;
  28. self.pausetimer = setTimeout(self.Next, self.options.pause);
  29. } else {
  30. self.CTL++;
  31. self.oUl.get(0).scrollTop++;
  32. }
  33. }, self.options.time);
  34. }
  35. //向左
  36. this.Left = function() {
  37. if (!self.canscroll) return false;
  38. clearTimeout(self.pausetimer);
  39. clearInterval(self.steptimer);
  40. var width = self.oUl.find("li:first").width();
  41. self.steptimer = setInterval(function(){
  42. if (self.CTL >= width) {
  43. self.CTL = 0;
  44. clearInterval(self.steptimer);
  45. self.oUl.find("li:first").appendTo(self.oUl);
  46. self.oUl.scrollLeft = 0;
  47. self.pausetimer = setTimeout(self.Left, self.options.pause);
  48. } else {
  49. self.CTL++;
  50. self.oUl.scrollLeft--;
  51. }
  52. }, self.options.time);
  53. }
  54. //上一个
  55. this.GoPrev = function() {
  56. if (!self.canscroll) return false;
  57. if (self.CTL > 0) {
  58. self.CTL = 0;
  59. self.oUl.get(0).scrollTop = 0;
  60. } else {
  61. self.oUl.find("li:last").prependTo(self.oUl);
  62. }
  63. }
  64. //跳到第一个
  65. this.GoFirst = function() {
  66. if (!self.canscroll) return false;
  67. while(!self.oUl.find("li:first").hasClass("first")) {
  68. this.GoPrev();
  69. }
  70. }
  71. //停止
  72. this.Stop = function() {
  73. clearTimeout(self.pausetimer);
  74. clearInterval(self.steptimer);
  75. }
  76. //增加一个LI
  77. this.Append = function(li, limit, type) {
  78. limit = limit - 1;
  79. self.oOUl.find(".first").removeClass("first").before(li);
  80. if (type == 'new') {
  81. self.oOUl.find(".speaker_new").remove();
  82. self.oOUl.find("li.mine:gt("+(limit)+")").remove();
  83. } else {
  84. self.oOUl.find(".speaker_user").remove();
  85. self.oOUl.find(".first").nextAll("li:gt("+(limit-1)+")").find(".speaker_new").remove();
  86. }
  87. self.oOUl.find("li.nohas").remove();
  88. self.Stop();
  89. self.Start();
  90. }
  91. this.Start = function() {
  92. //数据是否够滚动
  93. self.canscroll = (self.oOUl.find('li').length > self.options.row);
  94. if (self.canscroll) {
  95. self.oUl.html(self.oOUl.html()+self.oOUl.html());
  96. } else {
  97. self.oUl.html(self.oOUl.html());
  98. }
  99. switch (self.options.direct) {
  100. case 0 :
  101. self.pausetimer = setTimeout(self.Next, self.options.pause);
  102. break;
  103. case 1 :
  104. self.pausetimer = setTimeout(self.Prev, self.options.pause);
  105. break;
  106. case 2 :
  107. self.pausetimer = setTimeout(self.Left, self.options.pause);
  108. break;
  109. case 3 :
  110. self.pausetimer = setTimeout(self.Right, self.options.pause);
  111. break;
  112. }
  113. }
  114. this.oUl.mouseover(function(){self.Stop();});
  115. this.oUl.mouseout(function(){self.Next();});
  116. this.Start();
  117. }