pufferSlider.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. +function ($) {
  2. $.fn.pufferSlider = function (options) {
  3. if(this.length == 0) return this;
  4. if(this.length > 1){
  5. this.each(function(){$(this).pufferSlider(options)});
  6. return this;
  7. }
  8. var $this=$(this),
  9. defaults={
  10. mainCell : 'ul>li', // 大图数据Dom节点
  11. autoPlay : false, // 自动播放
  12. interTime : 3000, // 自动运行间隔
  13. autoPage : true, // 自动分页
  14. effect : false, // 是否带循环轮播
  15. startBack : null, // 每次切换效果开始时执行函数
  16. endBack : null, // 每次切换效果结束时执行函数
  17. },
  18. opts = $.extend(defaults, options),
  19. $dom = $this.find(opts.mainCell),
  20. Length = $dom.length,
  21. $wrap = $pic = $em = $exit = $prev = $next = $play = null,
  22. picTimer = null,
  23. photoTime = false,
  24. picI = 0,
  25. _init = function(){
  26. if($wrap){
  27. $wrap.show();
  28. }else{
  29. $wrap=$('<div class="photo-swipe-wrap" />').appendTo('body');
  30. $('<div class="photo-swipe-number"><span>/'+Length+'</span></div>').appendTo($wrap);
  31. if(opts.autoPage){
  32. $em=$('<em>'+(picI+1)+'</em>').prependTo($wrap.find('.photo-swipe-number>span'));
  33. }
  34. $pic=$('<div class="photo-swipe-pic" />').appendTo($wrap);
  35. var photoHtml='';
  36. for (var i=0; i<Length; i++){
  37. photoHtml+='<div><p><img src="'+$dom.eq(i).data('img')+'" /></p></div>';
  38. }
  39. $pic.html(photoHtml);
  40. $('<div class="photo-swipe-btn" />').appendTo($wrap);
  41. $exit=$('<span class="exit"><i></i></span>').appendTo($wrap.find('.photo-swipe-btn')).bind('click',function(){
  42. $wrap.hide()
  43. if(picTimer){clearTimeout(picTimer);}
  44. });
  45. $play=$('<span class="play"><i></i></span>').appendTo($wrap.find('.photo-swipe-btn')).bind('click',function(){
  46. autoPlayFun();
  47. });
  48. $prev=$('<span class="prev"><i></i></span>').appendTo($wrap.find('.photo-swipe-btn')).bind('click',function(){
  49. if(picI-1>-1){
  50. picI--
  51. _photoSlider();
  52. }
  53. });
  54. $next=$('<span class="next"><i></i></span>').appendTo($wrap.find('.photo-swipe-btn')).bind('click',function(){
  55. if(picI+1<$pic.children('div').length){
  56. picI++
  57. _photoSlider();
  58. }
  59. });
  60. _photoResize();
  61. if(opts.autoPlay){
  62. autoPlayFun();
  63. }
  64. function autoPlayFun(){
  65. if(photoTime){
  66. photoTime=false;
  67. $play.removeClass('on');
  68. clearTimeout(picTimer);
  69. }else{
  70. photoTime=true;
  71. $play.addClass('on');
  72. picTimer=setTimeout(_photoAutoSlider,opts.interTime);
  73. }
  74. }
  75. $pic.on('touchstart touchmove touchend touchcancel webkitTransitionEnd', _eventPhotoHand);
  76. }
  77. _photoSlider();
  78. },
  79. _eventPhotoHand=function(e){
  80. switch (e.type) {
  81. case 'touchmove':
  82. _phototouchMove(e);
  83. break;
  84. case 'touchstart':
  85. _phototouchStart(e);
  86. break;
  87. case 'touchcancel':
  88. case 'touchend':
  89. _phototouchEnd();
  90. break;
  91. case 'webkitTransitionEnd':
  92. _transitionEnd();
  93. break;
  94. case 'ortchange':
  95. _photoResize();
  96. break;
  97. }
  98. },
  99. _phototouchStart=function(e) {
  100. op=({
  101. pageX: e.originalEvent.touches[0].pageX,
  102. X : 0
  103. });
  104. $pic.get(0).style.webkitTransitionDuration = '0ms';
  105. },
  106. _phototouchMove=function(e) {
  107. e.preventDefault();
  108. e.stopPropagation();
  109. op.X=e.originalEvent.touches[0].pageX - op.pageX;
  110. $pic.get(0).style.webkitTransform = 'translate3d(' + (op.X-picI*photoW) + 'px,0,0)';
  111. },
  112. _phototouchEnd=function() {
  113. var stepLength = op.X <= -100 ? Math.ceil(-op.X / photoW) : (op.X > 100) ? -Math.ceil(op.X / photoW) : 0;
  114. if(stepLength==1){
  115. if(picI<$pic.children('div').length-1){picI++}
  116. }else if (stepLength==-1){
  117. if(picI>0){picI--}
  118. }
  119. _photoSlider();
  120. },
  121. _photoSlider=function(){
  122. $pic.get(0).style.cssText += '-webkit-transition:400ms;-webkit-transform:translate3d(-' + (photoW*picI) + 'px,0,0);';
  123. if(opts.autoPage){
  124. $em.text(picI+1);
  125. }
  126. },
  127. _photoAutoSlider=function(){
  128. if(picI>=Length-1){
  129. picI=0
  130. }else{
  131. picI++
  132. }
  133. _photoSlider();
  134. picTimer=setTimeout(_photoAutoSlider,opts.interTime);
  135. },
  136. _transitionEnd=function(){
  137. //alert(3)
  138. },
  139. _photoResize=function(){
  140. if($wrap){
  141. var w=$(window).width(), h=$(window).height(),minH=h-70;
  142. photoW=w;
  143. $pic.height(minH);
  144. $pic.children('div').each(function(index){
  145. var CSS = {
  146. 'position' : 'absolute',
  147. 'height' : minH,
  148. 'width' : w,
  149. '-webkit-transform' : 'translate('+(index*w)+'px,0)',
  150. '-moz-transform' : 'translate('+(index*w)+'px,0)',
  151. '-o-transform' : 'translate('+(index*w)+'px,0)',
  152. '-ms-transform' : 'translate('+(index*w)+'px,0)',
  153. 'transform' : 'translate('+(index*w)+'px,0)',
  154. 'z-index' : 900
  155. }
  156. $(this).css(CSS).find('p').width(w).height(minH)
  157. })
  158. }
  159. };
  160. $dom.on('click',function(){
  161. picI=$(this).index();
  162. _init();
  163. });
  164. }
  165. }(jQuery);