zhiqim_slider.mobile.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
  3. *
  4. * 指定登记&发行网站: https://www.zhiqim.com/ 欢迎加盟知启蒙,[编程有你,知启蒙一路随行]。
  5. *
  6. * 本文采用《知启蒙登记发行许可证》,除非符合许可证,否则不可使用该文件!
  7. * 1、您可以免费使用、修改、合并、出版发行和分发,再授权软件、软件副本及衍生软件;
  8. * 2、您用于商业用途时,必须在原作者指定的登记网站,按原作者要求进行登记;
  9. * 3、您在使用、修改、合并、出版发行和分发时,必须包含版权声明、许可声明,及保留原作者的著作权、商标和专利等知识产权;
  10. * 4、您在互联网、移动互联网等大众网络下发行和分发再授权软件、软件副本及衍生软件时,必须在原作者指定的发行网站进行发行和分发;
  11. * 5、您可以在以下链接获取一个完整的许可证副本。
  12. *
  13. * 许可证链接:http://zhiqim.org/licenses/zhiqim_register_publish_license.htm
  14. *
  15. * 除非法律需要或书面同意,软件由原始码方式提供,无任何明示或暗示的保证和条件。详见完整许可证的权限和限制。
  16. */
  17. +(function(Z)
  18. {
  19. /****************************************/
  20. //“轮播图”
  21. /****************************************/
  22. Z.ZmSlider = Z.Class.newInstance();
  23. Z.ZmSlider.prototype =
  24. {
  25. //start of Z.ZmSlider.prototype
  26. defaults:
  27. {//定义
  28. elem: '', // 主容器
  29. rectRatio: 2, // 宽度比例
  30. playAuto: true, // 设定自动滚动
  31. playSpeed: 2000, // 播放间隔
  32. transDur: 300, // 动画时长
  33. transFun: "ease", // 动画轨迹
  34. },
  35. init: function()
  36. {//初始化
  37. },
  38. execute: function()
  39. {//执行
  40. //1:重新组装排版
  41. this.$touch = Z.$elem(this.elem, "Z.ZmSlider").addClass("z-slider-touch");
  42. this.$wrap = Z('<div class="z-slider z-bg-gray z-text-center"></div>').insertAfter(this.$touch);
  43. this.$wrap.append(this.$touch);
  44. this.$tab = Z('<div class="z-slider-tab"></div>').appendTo(this.$wrap);
  45. this.$$items = this.$touch.children("li");
  46. //2:基本参数
  47. this.width = this.$wrap.offsetWidth();
  48. this.height = this.width/this.rectRatio;
  49. this.length = this.$$items.length;
  50. if (this.rectRatio < 0){
  51. this.rectRatio = 2;
  52. }
  53. if (this.playSpeed < this.transDur){
  54. this.playSpeed = this.transDur;
  55. }
  56. //3:插入展示按钮html
  57. var btnsHtml = "";
  58. for(var i = 0;i < this.length;i++){
  59. btnsHtml += '<span></span>';
  60. }
  61. this.$tab.html(btnsHtml);
  62. this.$$tabs = this.$tab.children("span");
  63. //4:初始化样式、位置
  64. this.$wrap.css("height",this.height);
  65. if (this.length <= 1)
  66. {/*一张图片时*/
  67. return;
  68. }
  69. if (this.length == 2)
  70. {/*两个滚动素材的情况*/
  71. this.$touch.css("transition-timing-function",this.transFun).css("transition-duration",this.transDur+"ms")
  72. .css("transform","translate3d(0,0,0)");
  73. Z(this.$$items[0]).css("transform","translate3d(0,0,0)").addClass("z-active");
  74. Z(this.$$items[1]).css("transform","translate3d("+this.width+"px,0,0)");
  75. //要实现无缝滚动,需复制节点
  76. this.$firstClone = Z(this.$$items[0].cloneNode(true)).appendTo(this.$touch);
  77. this.$lastClone = Z(this.$$items[1].cloneNode(true)).appendTo(this.$touch);
  78. this.$firstClone.css("transform","translate3d("+2*this.width+"px,0,0)");
  79. this.$lastClone.css("transform","translate3d(-"+this.width+"px,0,0)");
  80. //动画结束,位置调整
  81. this.$touch.on(this.transitionFixed(), this.doubleSlideEnd, this);
  82. }
  83. else
  84. {/*两张以上滚动素材的情况*/
  85. this.$$items.css("transition-timing-function",this.transFun).css("transition-duration",this.transDur+"ms");
  86. var that = this;
  87. Z.each(this.$$items, function($item,index){
  88. var translateX = index * that.width + "px";
  89. if (index == 0) {
  90. Z($item).addClass("z-active");
  91. }
  92. if (index == that.length - 1) {
  93. translateX = -that.width + "px";
  94. }
  95. Z($item).css("transform","translate3d("+translateX+",0,0)");
  96. });
  97. }
  98. Z(this.$$tabs[0]).addClass("z-active");
  99. //5:滑动事件绑定
  100. this.touchmoving = false;
  101. this.$touch.on("touchstart",this.touchStart,this);
  102. this.$touch.on("touchmove",this.touchMove,this);
  103. this.$touch.on("touchend",this.touchEnd,this);
  104. //6:开始执行动画
  105. this.autoPlay();
  106. },
  107. autoPlay: function()
  108. {
  109. if(this.playAuto){
  110. var that = this;
  111. that.timer = setInterval(function(){
  112. that.sliderNext(that);
  113. },that.playSpeed);
  114. }
  115. },
  116. touchStart:function(ev)
  117. {//触摸事件开始
  118. Z.E.forbidden(ev);
  119. var touch = ev.touches[0];
  120. this.touchStartX = touch.pageX;
  121. //清除自动
  122. clearInterval(this.timer);
  123. //两个滚动素材的情况
  124. if (this.length == 2){
  125. var step = +step;
  126. var $activeItem = this.$touch.find(".z-active");
  127. var activeIndex = Z.AR.indexOf(this.$$items, $activeItem[0]);
  128. this.touchBase = -activeIndex*this.width;
  129. this.$touch.css("transition-property","none");
  130. return;
  131. }
  132. //当前、前一个、后一个
  133. this.$activeItem = this.$touch.find(".z-active");
  134. var activeIndex = Z.AR.indexOf(this.$$items, this.$activeItem[0]);
  135. this.$activeNext = Z(this.$$items[activeIndex + 1] || this.$$items[0]);
  136. this.$activePrev = Z(this.$$items[activeIndex - 1] || this.$$items[this.length - 1]);
  137. //清除transition
  138. this.$activeItem.css("transition-duration","0ms");
  139. this.$activeNext.css("transition-duration","0ms");
  140. this.$activePrev.css("transition-duration","0ms");
  141. },
  142. touchMove: function(ev)
  143. {//滑动中
  144. if(this.touchmoving == false){
  145. this.touchmoving = true;
  146. }
  147. Z.E.forbidden(ev);
  148. var touch = ev.touches[0];
  149. this.touchMoveX = touch.pageX;
  150. //设置translate
  151. var metabolicX = this.touchMoveX - this.touchStartX;
  152. //两个滚动素材的情况
  153. if (this.length == 2){
  154. this.$touch.css("transform","translate3d("+(metabolicX+this.touchBase)+"px,0,0)");
  155. return;
  156. }
  157. this.$activeItem.css("transform","translate3d("+metabolicX+"px,0,0)");
  158. this.$activeNext.css("transform","translate3d("+(metabolicX + this.width)+"px,0,0)");
  159. this.$activePrev.css("transform","translate3d("+(metabolicX - this.width)+"px,0,0)");
  160. },
  161. touchEnd: function(ev)
  162. {//触摸事件结束
  163. if (this.touchmoving == false){
  164. return;
  165. }
  166. this.touchmoving = false;
  167. Z.E.forbidden(ev);
  168. //执行自动
  169. this.autoPlay();
  170. //判断滑动结果
  171. var metabolicX = this.touchMoveX - this.touchStartX;
  172. if (metabolicX == 0 || Math.abs(metabolicX) < this.width/2){
  173. this.sliderStay();
  174. return;
  175. }
  176. if (this.$activeItem) this.$activeItem.css("transition-duration",this.transDur+"ms");
  177. if (metabolicX < 0){
  178. if (this.$activeNext) this.$activeNext.css("transition-duration",this.transDur+"ms");
  179. this.sliderNext();
  180. } else {
  181. if (this.$activePrev) this.$activePrev.css("transition-duration",this.transDur+"ms");
  182. this.sliderPrev();
  183. }
  184. },
  185. sliderNext: function()
  186. {//后一个展示
  187. if (this.length == 2){
  188. this.doubleSlide(1);
  189. return;
  190. }
  191. this.sliderPlay(1);
  192. },
  193. sliderStay: function ()
  194. {//保持展示
  195. if (this.length == 2){
  196. this.doubleSlide(0);
  197. return;
  198. }
  199. this.sliderPlay(0);
  200. },
  201. sliderPrev: function()
  202. {//前一个展示
  203. if (this.length == 2){
  204. this.doubleSlide(-1);
  205. return;
  206. }
  207. this.sliderPlay(-1);
  208. },
  209. sliderPlay: function(step)
  210. {//滑动方法
  211. var step = +step;
  212. var $activeItem = this.$touch.find(".z-active");
  213. var activeIndex = Z.AR.indexOf(this.$$items, $activeItem[0]);
  214. $activeItem.removeClass("z-active");
  215. //即将展示对象的索引值
  216. var shouldIndex = activeIndex + step;
  217. if (shouldIndex >= this.length) shouldIndex = 0;
  218. if (shouldIndex <= -1) shouldIndex = this.length - 1;
  219. //即将新定位的active,以及前后两个
  220. var $shouldActive = Z(this.$$items[shouldIndex]);
  221. var $shouldNext = Z(this.$$items[shouldIndex + 1] || this.$$items[0]);
  222. var $shouldPrev = Z(this.$$items[shouldIndex - 1] || this.$$items[this.length - 1]);
  223. //touch item新定位
  224. $shouldActive.addClass("z-active").css("transform","translate3d(0,0,0)")
  225. .css("transition-duration",this.transDur+"ms");
  226. $shouldNext.css("transform","translate3d("+this.width+"px,0,0)");
  227. $shouldPrev.css("transform","translate3d(-"+this.width+"px,0,0)");
  228. //前后两个判断是否动画
  229. var nextDur = prevDur = this.transDur+"ms";
  230. if (step > 0){ nextDur = "0ms";}
  231. else if(step < 0){ prevDur = "0ms";}
  232. $shouldNext.css("transition-duration",nextDur);
  233. $shouldPrev.css("transition-duration",prevDur);
  234. //btn 展示
  235. Z(this.$$tabs[shouldIndex]).addClass("z-active")
  236. .siblings("span").removeClass("z-active");
  237. },
  238. doubleSlide: function(step)
  239. {//两个滚动素材的动画
  240. var step = +step;
  241. var $activeItem = this.$touch.find(".z-active");
  242. var activeIndex = Z.AR.indexOf(this.$$items, $activeItem[0]);
  243. $activeItem.removeClass("z-active");
  244. var num = shouldIndex = activeIndex + step;
  245. if (shouldIndex >= this.length) shouldIndex = 0;
  246. if (shouldIndex <= -1) shouldIndex = this.length - 1;
  247. Z(this.$$items[shouldIndex]).addClass("z-active");
  248. this.$touch.css("transform", "translate3d("+(-num*this.width)+"px,0,0)")
  249. .css("transition-property","transform");
  250. //btn 展示
  251. Z(this.$$tabs[shouldIndex]).addClass("z-active")
  252. .siblings("span").removeClass("z-active");
  253. },
  254. doubleSlideEnd: function()
  255. {//两图片的滚动结束
  256. var $activeItem = this.$touch.find(".z-active");
  257. var activeIndex = Z.AR.indexOf(this.$$items, $activeItem[0]);
  258. this.$touch.css("transition-property","none");
  259. if(activeIndex == 0){
  260. this.$touch.css("transform", "translate3d(0,0,0)");
  261. }
  262. if(activeIndex == 1){
  263. this.$touch.css("transform", "translate3d("+(-this.width)+"px,0,0)");
  264. }
  265. },
  266. transitionFixed: function()
  267. {//兼容写法
  268. var $div = document.createElement('div');
  269. if ($div.style["transition"] !== undefined ){
  270. $div = null;
  271. return "transitionend";
  272. }
  273. if ($div.style["OTransition"] !== undefined ){
  274. $div = null;
  275. return "oTransitionEnd";
  276. }
  277. if ($div.style["WebkitTransition"] !== undefined ){
  278. $div = null;
  279. return "webkitTransitionEnd";
  280. }
  281. },
  282. //end of Z.ZmSlider.prototype
  283. }
  284. //END
  285. })(zhiqim);