slider.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. !function(window) {
  2. "use strict";
  3. var doc = window.document
  4. , ydui = {};
  5. var util = ydui.util = {
  6. parseOptions: function(string) {},
  7. sessionStorage: function() {}(),
  8. serialize: function(value) {},
  9. deserialize: function(value) {}
  10. };
  11. function storage(ls) {}
  12. $.fn.emulateTransitionEnd = function(duration) {}
  13. ;
  14. if (typeof define === 'function') {
  15. define(ydui);
  16. } else {
  17. window.YDUI = ydui;
  18. }
  19. ;function Slider(element, options) {
  20. this.$element = $(element);
  21. this.options = $.extend({}, Slider.DEFAULTS, options || {});
  22. this.init();
  23. }
  24. Slider.DEFAULTS = {
  25. speed: 300,
  26. autoplay: 5000,
  27. lazyLoad: false,
  28. pagination: '.slider-pagination',
  29. wrapperClass: 'slider-wrapper',
  30. slideClass: 'slider-item',
  31. bulletClass: 'slider-pagination-item',
  32. bulletActiveClass: 'slider-pagination-item-active'
  33. };
  34. function Slider(element, options) {
  35. this.$element = $(element);
  36. this.options = $.extend({}, Slider.DEFAULTS, options || {});
  37. this.init();
  38. }
  39. Slider.DEFAULTS = {
  40. speed: 300,
  41. autoplay: 3000,
  42. lazyLoad: false,
  43. pagination: '.slider-pagination',
  44. wrapperClass: 'slider-wrapper',
  45. slideClass: 'slider-item',
  46. bulletClass: 'slider-pagination-item',
  47. bulletActiveClass: 'slider-pagination-item-active'
  48. };
  49. Slider.prototype.init = function() {
  50. var _this = this
  51. , options = _this.options
  52. , $element = _this.$element;
  53. _this.index = 1;
  54. _this.autoPlayTimer = null;
  55. _this.$pagination = $element.find(options.pagination);
  56. _this.$wrapper = $element.find('.' + options.wrapperClass);
  57. _this.itemNums = _this.$wrapper.find('.' + options.slideClass).length;
  58. options.lazyLoad && _this.loadImage(0);
  59. _this.createBullet();
  60. _this.cloneItem().bindEvent();
  61. }
  62. ;
  63. Slider.prototype.bindEvent = function() {
  64. var _this = this
  65. , touchEvents = _this.touchEvents();
  66. _this.$wrapper.find('.' + _this.options.slideClass).on(touchEvents.start, function(e) {
  67. _this.onTouchStart(e);
  68. }).on(touchEvents.move, function(e) {
  69. _this.onTouchMove(e);
  70. }).on(touchEvents.end, function(e) {
  71. _this.onTouchEnd(e);
  72. });
  73. $(window).on('resize.ydui.slider', function() {
  74. _this.setSlidesSize();
  75. });
  76. ~~_this.options.autoplay > 0 && _this.autoPlay();
  77. _this.$wrapper.on('click.ydui.slider', function(e) {
  78. if (!_this.touches.allowClick) {
  79. e.preventDefault();
  80. }
  81. });
  82. }
  83. ;
  84. Slider.prototype.cloneItem = function() {
  85. var _this = this
  86. , $wrapper = _this.$wrapper
  87. , $sliderItem = _this.$wrapper.find('.' + _this.options.slideClass)
  88. , $firstChild = $sliderItem.filter(':first-child').clone()
  89. , $lastChild = $sliderItem.filter(':last-child').clone();
  90. $wrapper.prepend($lastChild);
  91. $wrapper.append($firstChild);
  92. _this.setSlidesSize();
  93. return _this;
  94. }
  95. ;
  96. Slider.prototype.createBullet = function() {
  97. var _this = this;
  98. if (!_this.$pagination[0])
  99. return;
  100. var initActive = '<span class="' + (_this.options.bulletClass + ' ' + _this.options.bulletActiveClass) + '"></span>';
  101. _this.$pagination.append(initActive + new Array(_this.itemNums).join('<span class="' + _this.options.bulletClass + '"></span>'));
  102. }
  103. ;
  104. Slider.prototype.activeBullet = function() {
  105. var _this = this;
  106. if (!_this.$pagination[0])
  107. return;
  108. var itemNums = _this.itemNums
  109. , index = _this.index % itemNums >= itemNums ? 0 : _this.index % itemNums - 1
  110. , bulletActiveClass = _this.options.bulletActiveClass;
  111. !!_this.$pagination[0] && _this.$pagination.find('.' + _this.options.bulletClass).removeClass(bulletActiveClass).eq(index).addClass(bulletActiveClass);
  112. }
  113. ;
  114. Slider.prototype.setSlidesSize = function() {
  115. var _this = this
  116. , _width = _this.$wrapper.width();
  117. _this.$wrapper.css('transform', 'translate3d(-' + _width + 'px,0,0)');
  118. _this.$wrapper.find('.' + _this.options.slideClass).css({
  119. width: _width
  120. });
  121. }
  122. ;
  123. Slider.prototype.autoPlay = function() {
  124. var _this = this;
  125. _this.autoPlayTimer = setInterval(function() {
  126. if (_this.index > _this.itemNums) {
  127. _this.index = 1;
  128. _this.setTranslate(0, -_this.$wrapper.width());
  129. }
  130. _this.setTranslate(_this.options.speed, -(++_this.index * _this.$wrapper.width()));
  131. }, _this.options.autoplay);
  132. }
  133. ;
  134. Slider.prototype.stopAutoplay = function() {
  135. var _this = this;
  136. clearInterval(_this.autoPlayTimer);
  137. return _this;
  138. }
  139. ;
  140. Slider.prototype.setTranslate = function(speed, x) {
  141. var _this = this;
  142. _this.options.lazyLoad && _this.loadImage(_this.index);
  143. _this.activeBullet();
  144. _this.$wrapper.css({
  145. 'transitionDuration': speed + 'ms',
  146. 'transform': 'translate3d(' + x + 'px,0,0)'
  147. });
  148. }
  149. ;
  150. Slider.prototype.touches = {
  151. moveTag: 0,
  152. startClientX: 0,
  153. moveOffset: 0,
  154. touchStartTime: 0,
  155. isTouchEvent: false,
  156. allowClick: false// 用于判断事件为点击还是拖动
  157. };
  158. Slider.prototype.onTouchStart = function(event) {
  159. if (event.originalEvent.touches)
  160. event = event.originalEvent.touches[0];
  161. var _this = this
  162. , touches = _this.touches;
  163. touches.allowClick = true;
  164. touches.isTouchEvent = event.type === 'touchstart';
  165. if (!touches.isTouchEvent && 'which'in event && event.which === 3)
  166. return;
  167. if (touches.moveTag == 0) {
  168. touches.moveTag = 1;
  169. touches.startClientX = event.clientX;
  170. touches.touchStartTime = Date.now();
  171. var itemNums = _this.itemNums;
  172. if (_this.index == 0) {
  173. _this.index = itemNums;
  174. _this.setTranslate(0, -itemNums * _this.$wrapper.width());
  175. return;
  176. }
  177. if (_this.index > itemNums) {
  178. _this.index = 1;
  179. _this.setTranslate(0, -_this.$wrapper.width());
  180. }
  181. }
  182. }
  183. ;
  184. Slider.prototype.onTouchMove = function(event) {
  185. event.preventDefault();
  186. if (event.originalEvent.touches)
  187. event = event.originalEvent.touches[0];
  188. var _this = this
  189. , touches = _this.touches;
  190. touches.allowClick = false;
  191. if (touches.isTouchEvent && event.type === 'mousemove')
  192. return;
  193. var deltaSlide = touches.moveOffset = event.clientX - touches.startClientX;
  194. if (deltaSlide != 0 && touches.moveTag != 0) {
  195. if (touches.moveTag == 1) {
  196. _this.stopAutoplay();
  197. touches.moveTag = 2;
  198. }
  199. if (touches.moveTag == 2) {
  200. _this.setTranslate(0, -_this.index * _this.$wrapper.width() + deltaSlide);
  201. }
  202. }
  203. }
  204. ;
  205. Slider.prototype.onTouchEnd = function() {
  206. var _this = this
  207. , speed = _this.options.speed
  208. , _width = _this.$wrapper.width()
  209. , touches = _this.touches
  210. , moveOffset = touches.moveOffset;
  211. setTimeout(function() {
  212. touches.allowClick = true;
  213. }, 0);
  214. if (touches.moveTag == 1) {
  215. touches.moveTag = 0;
  216. }
  217. if (touches.moveTag == 2) {
  218. touches.moveTag = 0;
  219. var timeDiff = Date.now() - touches.touchStartTime;
  220. if (timeDiff > 300 && Math.abs(moveOffset) <= _this.$wrapper.width() * .5) {
  221. _this.setTranslate(speed, -_this.index * _this.$wrapper.width());
  222. } else {
  223. _this.setTranslate(speed, -((moveOffset > 0 ? --_this.index : ++_this.index) * _width));
  224. }
  225. _this.autoPlay();
  226. }
  227. }
  228. ;
  229. Slider.prototype.touchEvents = function() {
  230. var supportTouch = (window.Modernizr && !!window.Modernizr.touch) || (function() {
  231. return !!(('ontouchstart'in window) || window.DocumentTouch && document instanceof DocumentTouch);
  232. }
  233. )();
  234. return {
  235. start: supportTouch ? 'touchstart.ydui.slider' : 'mousedown.ydui.slider',
  236. move: supportTouch ? 'touchmove.ydui.slider' : 'mousemove.ydui.slider',
  237. end: supportTouch ? 'touchend.ydui.slider' : 'mouseup.ydui.slider'
  238. };
  239. }
  240. ;
  241. function Plugin(option) {
  242. return this.each(function() {
  243. var $this = $(this)
  244. , slider = $this.data('ydui.slider');
  245. if (!slider) {
  246. $this.data('ydui.slider', new Slider(this,option));
  247. }
  248. });
  249. }
  250. $(window).on('load.ydui.slider', function() {
  251. $('[data-ydui-slider]').each(function() {
  252. var $this = $(this);
  253. $this.slider(window.YDUI.util.parseOptions($this.data('ydui-slider')));
  254. });
  255. });
  256. $.fn.slider = Plugin;
  257. }(window);