index.wxml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <wxs module="swipe">
  2. var THRESHOLD = 0.3;
  3. var MIN_DISTANCE = 10;
  4. var owner;
  5. var state;
  6. var getState = function(ownerInstance) {
  7. owner = ownerInstance;
  8. state = owner.getState();
  9. state.leftWidth = state.leftWidth || 0;
  10. state.rightWidth = state.rightWidth || 0;
  11. state.offset = state.offset || 0;
  12. state.startOffset = state.startOffset || 0;
  13. };
  14. var initRightWidth = function(newVal, oldVal, ownerInstance) {
  15. getState(ownerInstance);
  16. state.rightWidth = newVal;
  17. if (state.offset < 0) {
  18. swipeMove(-state.rightWidth);
  19. }
  20. };
  21. var initLeftWidth = function(newVal, oldVal, ownerInstance) {
  22. getState(ownerInstance);
  23. state.leftWidth = newVal;
  24. if (state.offset > 0) {
  25. swipeMove(state.leftWidth);
  26. }
  27. }
  28. var resetTouchStatus = function() {
  29. state.direction = '';
  30. state.deltaX = 0;
  31. state.deltaY = 0;
  32. state.offsetX = 0;
  33. state.offsetY = 0;
  34. };
  35. var touchMove = function(event) {
  36. var touchPoint = event.touches[0];
  37. state.deltaX = touchPoint.clientX - state.startX;
  38. state.deltaY = touchPoint.clientY - state.startY;
  39. state.offsetX = Math.abs(state.deltaX);
  40. state.offsetY = Math.abs(state.deltaY);
  41. state.direction = state.direction || getDirection(state.offsetX, state.offsetY);
  42. };
  43. var getDirection = function(x, y) {
  44. if (x > y && x > MIN_DISTANCE) {
  45. return 'horizontal';
  46. }
  47. if (y > x && y > MIN_DISTANCE) {
  48. return 'vertical';
  49. }
  50. return '';
  51. };
  52. var range = function(num, min, max) {
  53. return Math.min(Math.max(num, min), max);
  54. };
  55. var swipeMove = function(_offset = 0) {
  56. state.offset = range(
  57. _offset,
  58. -state.rightWidth,
  59. +state.leftWidth,
  60. );
  61. var transform = 'translate3d(' + state.offset + 'px, 0, 0)';
  62. var transition = state.dragging
  63. ? 'none'
  64. : 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  65. owner.selectComponent('#wrapper').setStyle({
  66. '-webkit-transform': transform,
  67. '-webkit-transition': transition,
  68. 'transform': transform,
  69. 'transition': transition
  70. });
  71. };
  72. var close = function() {
  73. swipeMove(0);
  74. };
  75. var onCloseChange = function(newVal, oldVal, ownerInstance) {
  76. getState(ownerInstance);
  77. if (newVal === oldVal) return;
  78. if (newVal) {
  79. close();
  80. }
  81. };
  82. var touchStart = function(event) {
  83. resetTouchStatus();
  84. state.startOffset = state.offset;
  85. var touchPoint = event.touches[0];
  86. state.startX = touchPoint.clientX;
  87. state.startY = touchPoint.clientY;
  88. owner.callMethod('closeOther');
  89. };
  90. var startDrag = function(event, ownerInstance) {
  91. getState(ownerInstance);
  92. touchStart(event);
  93. };
  94. var onDrag = function(event, ownerInstance) {
  95. getState(ownerInstance);
  96. touchMove(event);
  97. if (state.direction !== 'horizontal') {
  98. return;
  99. }
  100. state.dragging = true;
  101. swipeMove(state.startOffset + state.deltaX);
  102. };
  103. var open = function(position) {
  104. var _offset = position === 'left' ? +state.leftWidth : -state.rightWidth;
  105. owner.callMethod('open', { position: position });
  106. swipeMove(_offset);
  107. };
  108. var endDrag = function(event, ownerInstance) {
  109. getState(ownerInstance);
  110. state.dragging = false;
  111. // 左/右侧有可滑动区域,且当前不是已open状态,且滑动幅度超过阈值时open左/右侧(滚动到该侧的最边上)
  112. if (+state.rightWidth > 0 && -state.startOffset < +state.rightWidth && -state.offset > +state.rightWidth * THRESHOLD) {
  113. open('right');
  114. } else if (+state.leftWidth > 0 && state.startOffset < +state.leftWidth && state.offset > +state.leftWidth * THRESHOLD) {
  115. open('left');
  116. } else {
  117. // 仅在有发生侧滑的情况下自动关闭(由js控制是否异步关闭)
  118. if (state.startOffset !== state.offset) {
  119. close();
  120. }
  121. }
  122. };
  123. module.exports = {
  124. initLeftWidth: initLeftWidth,
  125. initRightWidth: initRightWidth,
  126. startDrag: startDrag,
  127. onDrag: onDrag,
  128. endDrag: endDrag,
  129. onCloseChange: onCloseChange
  130. };
  131. </wxs>
  132. <view
  133. class="wr-class wr-swipeout"
  134. data-key="cell"
  135. capture-bind:tap="onClick"
  136. bindtouchstart="{{disabled || swipe.startDrag}}"
  137. capture-bind:touchmove="{{disabled || swipe.onDrag}}"
  138. bindtouchend="{{disabled || swipe.endDrag}}"
  139. bindtouchcancel="{{disabled || swipe.endDrag}}"
  140. closed="{{closed}}"
  141. change:closed="{{swipe.onCloseChange}}"
  142. leftWidth="{{leftWidth}}"
  143. rightWidth="{{rightWidth}}"
  144. change:leftWidth="{{swipe.initLeftWidth}}"
  145. change:rightWidth="{{swipe.initRightWidth}}"
  146. >
  147. <view id="wrapper">
  148. <view wx:if="{{ leftWidth }}" class="wr-swipeout__left" data-key="left" catch:tap="onClick">
  149. <slot name="left" />
  150. </view>
  151. <slot />
  152. <view wx:if="{{ rightWidth }}" class="wr-swipeout__right" data-key="right" catch:tap="onClick">
  153. <slot name="right" />
  154. </view>
  155. </view>
  156. </view>