slider.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * slider插件可悬停控制
  3. */
  4. ; $(function ($, window, document, undefined) {
  5. Slider = function (container, options) {
  6. /*
  7. options = {
  8. auto: true,
  9. time: 3000,
  10. event: 'hover' | 'click',
  11. mode: 'slide | fade',
  12. controller: $(),
  13. activeControllerCls: 'className',
  14. exchangeEnd: $.noop
  15. }
  16. */
  17. "use strict"; //stirct mode not support by IE9-
  18. if (!container) return;
  19. var options = options || {},
  20. currentIndex = 0,
  21. cls = options.activeControllerCls,
  22. delay = options.delay,
  23. isAuto = options.auto,
  24. controller = options.controller,
  25. event = options.event,
  26. interval,
  27. slidesWrapper = container.children().first(),
  28. slides = slidesWrapper.children(),
  29. length = slides.length,
  30. childWidth = container.width(),
  31. totalWidth = childWidth * slides.length;
  32. function init() {
  33. var controlItem = controller.children();
  34. mode();
  35. event == 'hover' ? controlItem.mouseover(function () {
  36. stop();
  37. var index = $(this).index();
  38. play(index, options.mode);
  39. }).mouseout(function () {
  40. isAuto && autoPlay();
  41. }) : controlItem.click(function () {
  42. stop();
  43. var index = $(this).index();
  44. play(index, options.mode);
  45. isAuto && autoPlay();
  46. });
  47. isAuto && autoPlay();
  48. }
  49. //animate mode
  50. function mode() {
  51. var wrapper = container.children().first();
  52. options.mode == 'slide' ? wrapper.width(totalWidth) : wrapper.children().css({
  53. 'position': 'absolute',
  54. 'left': 0,
  55. 'top': 0
  56. })
  57. .first().siblings().hide();
  58. }
  59. //auto play
  60. function autoPlay() {
  61. interval = setInterval(function () {
  62. triggerPlay(currentIndex);
  63. }, options.time);
  64. }
  65. //trigger play
  66. function triggerPlay(cIndex) {
  67. var index;
  68. (cIndex == length - 1) ? index = 0 : index = cIndex + 1;
  69. play(index, options.mode);
  70. }
  71. //play
  72. function play(index, mode) {
  73. slidesWrapper.stop(true, true);
  74. slides.stop(true, true);
  75. mode == 'slide' ? (function () {
  76. if (index > currentIndex) {
  77. slidesWrapper.animate({
  78. left: '-=' + Math.abs(index - currentIndex) * childWidth + 'px'
  79. }, delay);
  80. } else if (index < currentIndex) {
  81. slidesWrapper.animate({
  82. left: '+=' + Math.abs(index - currentIndex) * childWidth + 'px'
  83. }, delay);
  84. } else {
  85. return;
  86. }
  87. })() : (function () {
  88. if (slidesWrapper.children(':visible').index() == index) return;
  89. slidesWrapper.children().fadeOut(delay).eq(index).fadeIn(delay);
  90. })();
  91. try {
  92. controller.children('.' + cls).removeClass(cls);
  93. controller.children().eq(index).addClass(cls);
  94. } catch (e) { }
  95. currentIndex = index;
  96. options.exchangeEnd && typeof options.exchangeEnd == 'function' && options.exchangeEnd.call(this, currentIndex);
  97. }
  98. //stop
  99. function stop() {
  100. clearInterval(interval);
  101. }
  102. //prev frame
  103. function prev() {
  104. stop();
  105. currentIndex == 0 ? triggerPlay(length - 2) : triggerPlay(currentIndex - 2);
  106. isAuto && autoPlay();
  107. }
  108. //next frame
  109. function next() {
  110. stop();
  111. currentIndex == length - 1 ? triggerPlay(-1) : triggerPlay(currentIndex);
  112. isAuto && autoPlay();
  113. }
  114. //init
  115. init();
  116. //expose the Slider API
  117. return {
  118. prev: function () {
  119. prev();
  120. },
  121. next: function () {
  122. next();
  123. }
  124. }
  125. };
  126. }(jQuery, window, document));