scroll.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. (function($) {
  2. var timers = [];
  3. function nextMove(obj, step) {
  4. obj.find("ul").animate({
  5. marginLeft: -step
  6. },
  7. 300, 'swing',
  8. function() {
  9. $(this).find("li").slice(0, 1).appendTo($(this));
  10. $(this).css("margin-left", 0);
  11. });
  12. }
  13. function preMove(obj, step) {
  14. var ul = obj.find("ul");
  15. ul.css('margin-left', -step);
  16. ul.find("li").last().prependTo(ul);
  17. ul.animate({
  18. marginLeft: 0
  19. },
  20. 300, 'swing',
  21. function() {
  22. ul.css("margin-left", 0);
  23. });
  24. }
  25. function downMove(obj, step) {
  26. obj.find("ul").animate({
  27. marginTop: -step
  28. },
  29. 300, 'swing',
  30. function() {
  31. $(this).find("li").slice(0, 1).appendTo($(this));
  32. $(this).css("margin-top", 0);
  33. });
  34. }
  35. function upMove(obj, step) {
  36. var ul = obj.find("ul");
  37. ul.find("li").last().prependTo(ul);
  38. ul.css('margin-top', -step);
  39. ul.animate({
  40. marginTop: 0
  41. },
  42. 300, 'swing',
  43. function() {
  44. ul.css("margin-top", 0);
  45. });
  46. }
  47. var methods = {
  48. init: function(options) {
  49. var defaults = {
  50. speed: 3000,
  51. direction: 'horizantal'
  52. };
  53. var opts = $.extend({},
  54. defaults, options);
  55. return this.each(function(i) {
  56. var speed = opts["speed"] < 1000 ? 3000 : opts["speed"];
  57. var direction = opts["direction"] == 'vertical' || opts["direction"] == 'horizantal' ? opts["direction"] : 'vertical';
  58. var _this = $(this);
  59. var ul = _this.find("ul"),
  60. pre = _this.find(".pre"),
  61. next = _this.find(".next");
  62. var sh, isMove, ishori, move, lmove, rmove;
  63. if (direction == 'horizantal') {
  64. isMove = _this.width() < ul.width();
  65. ishori = true;
  66. move = nextMove;
  67. lmove = preMove;
  68. rmove = nextMove;
  69. sh = ul.find("li:first").outerWidth(true);
  70. } else {
  71. isMove = _this.height() < ul.height();
  72. ishori = false;
  73. move = downMove;
  74. lmove = upMove;
  75. rmove = downMove;
  76. sh = ul.find("li:first").outerHeight(true);
  77. }
  78. if (isMove) {
  79. timers[i] = setInterval(function() {
  80. move(_this, sh);
  81. },
  82. speed);
  83. _this.hover(function() {
  84. clearInterval(timers[i]);
  85. },
  86. function() {
  87. timers[i] = setInterval(function() {
  88. move(_this, sh);
  89. },
  90. speed);
  91. });
  92. pre.click(function() {
  93. lmove(_this, sh);
  94. });
  95. next.click(function() {
  96. rmove(_this, sh);
  97. });
  98. }
  99. });
  100. },
  101. destroy: function() {
  102. return this.each(function(i) {
  103. _this = $(this);
  104. clearInterval(timers[i]);
  105. _this.find('ul').css({
  106. 'margin-top': 0
  107. });
  108. _this.find('ul').css({
  109. 'margin-left': 0
  110. });
  111. _this.unbind("mouseenter").unbind("mouseleave");
  112. _this.find('.pre').unbind('click');
  113. _this.find('.next').unbind('click');
  114. });
  115. }
  116. }
  117. $.fn.mySingleScroll = function(options) {
  118. var method = arguments[0];
  119. if (methods[method]) {
  120. method = methods[method];
  121. } else if (typeof method == 'object' || !method) {
  122. method = methods.init;
  123. } else {
  124. $.error('error');
  125. return this;
  126. }
  127. return method.apply(this, arguments);
  128. }
  129. })(jQuery)