swiper.wxs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * 轮播触屏脚本
  3. */
  4. // 轮播容器ID
  5. var containerId = '#swiperContainer';
  6. // 移动阈值
  7. var moveThreshold = 0.3;
  8. // 轮播方向
  9. var DIRECTION = {
  10. // 水平
  11. HOR: 'horizontal',
  12. // 垂直
  13. VER: 'vertical',
  14. };
  15. function updateMoveInfo(state, event) {
  16. var touchPoint = event.touches[0];
  17. state.moveX = touchPoint.clientX - state.startX;
  18. state.moveY = touchPoint.clientY - state.startY;
  19. }
  20. function moveContainer(state, reset) {
  21. if (state.direction === DIRECTION.HOR) {
  22. var offset = reset ? state.offsetX : state.offsetX + state.moveX;
  23. // console.log(DIRECTION.HOR, state.moveX);
  24. if (offset > 0) {
  25. offset = Math.min(state.width * moveThreshold, offset);
  26. } else {
  27. offset = Math.max(-state.width * (state.total - (1 - moveThreshold)), offset);
  28. }
  29. setOffset(state, offset, 0, !reset);
  30. } else {
  31. var offset = reset ? state.offsetY : state.offsetY + state.moveY;
  32. // console.log(DIRECTION.VER, state.moveY);
  33. if (offset > 0) {
  34. offset = Math.min(state.height * moveThreshold, offset);
  35. } else {
  36. offset = Math.max(-state.height * (state.total - (1 - moveThreshold)), offset);
  37. }
  38. setOffset(state, 0, offset, !reset);
  39. }
  40. }
  41. function setOffset(state, offsetX, offsetY, disAni) {
  42. var transform = 'translate3d(' + (offsetX || 0) + 'px, ' + (offsetY || 0) + 'px, 0)';
  43. var styles = {
  44. // '-webkit-transform': transform,
  45. transform: transform,
  46. };
  47. if (disAni) {
  48. styles['transition'] = 'none';
  49. }
  50. state.container.setStyle(styles);
  51. }
  52. function initContainer(ownerInstance) {
  53. var state = ownerInstance.getState();
  54. state.container = state.container || ownerInstance.selectComponent(containerId);
  55. }
  56. function startDrag(event, ownerInstance) {
  57. initContainer(ownerInstance);
  58. var state = ownerInstance.getState();
  59. var touchPoint = event.touches[0];
  60. state.moveX = 0;
  61. state.moveY = 0;
  62. state.startX = touchPoint.clientX;
  63. state.startY = touchPoint.clientY;
  64. ownerInstance.callMethod('pause');
  65. }
  66. function onDrag(event, ownerInstance) {
  67. var state = ownerInstance.getState();
  68. state.dragging = true;
  69. updateMoveInfo(state, event);
  70. moveContainer(state);
  71. }
  72. function endDrag(event, ownerInstance) {
  73. var state = ownerInstance.getState();
  74. state.dragging = false;
  75. ownerInstance.callMethod('replay');
  76. var pageDir = '';
  77. if (state.direction === DIRECTION.HOR) {
  78. pageDir = getPageDir(state.moveX, state.width);
  79. } else {
  80. pageDir = getPageDir(state.moveY, state.height);
  81. }
  82. if (pageDir) {
  83. ownerInstance.callMethod(pageDir, { source: 'touch' });
  84. return;
  85. }
  86. // 重置
  87. moveContainer(state, true);
  88. }
  89. /**
  90. * 返回分页操作的方向
  91. * @param move 移动距离
  92. * @param size 容器尺寸
  93. * @returns
  94. */
  95. function getPageDir(move, size) {
  96. if (move > 0) {
  97. // 右滑超过阈值
  98. if (move > size * moveThreshold) {
  99. return 'prev';
  100. }
  101. } else {
  102. // 左滑超过阈值
  103. if (-move > size * moveThreshold) {
  104. return 'next';
  105. }
  106. }
  107. }
  108. function changeOffsetByDirection(direction, newVal, ownerInstance) {
  109. initContainer(ownerInstance);
  110. var state = ownerInstance.getState();
  111. if (state.direction !== direction) return;
  112. var needInit = !state.inited && state.currentInited;
  113. if (direction === DIRECTION.HOR) {
  114. setOffset(state, state.offsetX, 0, needInit);
  115. } else {
  116. setOffset(state, 0, state.offsetY, needInit);
  117. }
  118. if (needInit) {
  119. state.inited = true;
  120. ownerInstance.callMethod('inited');
  121. }
  122. }
  123. function changeOffsetX(newVal, oldVal, ownerInstance) {
  124. var state = ownerInstance.getState();
  125. state.offsetX = newVal;
  126. changeOffsetByDirection(DIRECTION.HOR, newVal, ownerInstance);
  127. }
  128. function changeOffsetY(newVal, oldVal, ownerInstance) {
  129. var state = ownerInstance.getState();
  130. state.offsetY = newVal;
  131. changeOffsetByDirection(DIRECTION.VER, newVal, ownerInstance);
  132. }
  133. function changeWidth(newVal, oldVal, ownerInstance) {
  134. var state = ownerInstance.getState();
  135. state.width = newVal;
  136. }
  137. function changeHeight(newVal, oldVal, ownerInstance) {
  138. var state = ownerInstance.getState();
  139. state.height = newVal;
  140. }
  141. function changeDirection(newVal, oldVal, ownerInstance) {
  142. var state = ownerInstance.getState();
  143. state.direction = newVal;
  144. if (oldVal && newVal !== oldVal) {
  145. state.inited = false;
  146. changeOffsetByDirection(newVal, state.offsetY, ownerInstance);
  147. }
  148. }
  149. function changeTotal(newVal, oldVal, ownerInstance) {
  150. var state = ownerInstance.getState();
  151. state.total = newVal;
  152. }
  153. function changeCurrentInited(newVal, oldVal, ownerInstance) {
  154. var state = ownerInstance.getState();
  155. state.currentInited = newVal;
  156. }
  157. module.exports = {
  158. startDrag: startDrag,
  159. onDrag: onDrag,
  160. endDrag: endDrag,
  161. changeOffsetX: changeOffsetX,
  162. changeOffsetY: changeOffsetY,
  163. changeWidth: changeWidth,
  164. changeHeight: changeHeight,
  165. changeDirection: changeDirection,
  166. changeTotal: changeTotal,
  167. changeCurrentInited: changeCurrentInited,
  168. };