swipe-out.wxml 4.8 KB

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