index.wxs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * 此为wxs模块,只支持APP-VUE,微信和QQ小程序以及H5平台
  3. * wxs内部不支持es6语法,变量只能使用var定义,无法使用解构,箭头函数等特性
  4. */
  5. // 开始触摸
  6. function touchstart(event, ownerInstance) {
  7. // 触发事件的组件的ComponentDescriptor实例
  8. var instance = event.instance
  9. // wxs内的局部变量快照,此快照是属于整个组件的,在touchstart和touchmove事件中都能获取到相同的结果
  10. var state = instance.getState()
  11. if (state.disabled) return
  12. var touches = event.touches
  13. // 如果进行的是多指触控,不允许进行操作
  14. if (touches && touches.length > 1) return
  15. // 标识当前为滑动中状态
  16. state.moving = true
  17. // 记录触摸开始点的坐标值
  18. state.startX = touches[0].pageX
  19. state.startY = touches[0].pageY
  20. ownerInstance.callMethod('closeOther')
  21. }
  22. // 触摸滑动
  23. function touchmove(event, ownerInstance) {
  24. // 触发事件的组件的ComponentDescriptor实例
  25. var instance = event.instance
  26. // wxs内的局部变量快照
  27. var state = instance.getState()
  28. if (state.disabled || !state.moving) return
  29. var touches = event.touches
  30. var pageX = touches[0].pageX
  31. var pageY = touches[0].pageY
  32. var moveX = pageX - state.startX
  33. var moveY = pageY - state.startY
  34. var buttonsWidth = state.buttonsWidth
  35. // 移动的X轴距离大于Y轴距离,也即终点与起点位置连线,与X轴夹角小于45度时,禁止页面滚动
  36. if (Math.abs(moveX) > Math.abs(moveY) || Math.abs(moveX) > state.threshold) {
  37. event.preventDefault && event.preventDefault()
  38. event.stopPropagation && event.stopPropagation()
  39. }
  40. // 如果移动的X轴距离小于Y轴距离,也即终点位置与起点位置连线,与Y轴夹角小于45度时,认为是页面上下滑动,而不是左右滑动单元格
  41. if (Math.abs(moveX) < Math.abs(moveY)) return
  42. // 限制右滑的距离,不允许内容部分往右偏移,右滑会导致X轴偏移值大于0,以此做判断
  43. // 此处不能直接return,因为滑动过程中会缺失某些关键点坐标,会导致错乱,最好的办法就是
  44. // 在超出后,设置为0
  45. if (state.status === 'open') {
  46. // 在开启状态下,向左滑动,需忽略
  47. if (moveX < 0) moveX = 0
  48. // 想要收起菜单,最大能移动的距离为按钮的总宽度
  49. if (moveX > buttonsWidth) moveX = buttonsWidth
  50. // 如果是已经打开了的状态,向左滑动时,移动收起菜单
  51. moveSwipeAction(-buttonsWidth + moveX, instance, ownerInstance)
  52. } else {
  53. // 关闭状态下,右滑动需忽略
  54. if (moveX > 0) moveX = 0
  55. // 滑动的距离不允许超过所有按钮的总宽度,此时只能是左滑,最终设置按钮的总宽度,同时为负数
  56. if (Math.abs(moveX) > buttonsWidth) moveX = -buttonsWidth
  57. // 只要是在滑过程中,就不断移动单元格内容部分,从而使隐藏的菜单显示出来
  58. moveSwipeAction(moveX, instance, ownerInstance)
  59. }
  60. }
  61. // 触摸结束
  62. function touchend(event, ownerInstance) {
  63. // 触发事件的组件的ComponentDescriptor实例
  64. var instance = event.instance
  65. // wxs内的局部变量快照
  66. var state = instance.getState()
  67. if (!state.moving || state.disabled) return
  68. var touches = event.changedTouches ? event.changedTouches[0] : {}
  69. var pageX = touches.pageX
  70. var pageY = touches.pageY
  71. var moveX = pageX - state.startX
  72. if (state.status === 'open') {
  73. // 在展开的状态下,继续左滑,无需操作
  74. if (moveX < 0) return
  75. // 在开启状态下,点击一下内容区域,moveX为0,也即没有进行移动,这时执行收起菜单逻辑
  76. if (moveX === 0) {
  77. return closeSwipeAction(instance, ownerInstance)
  78. }
  79. // 在开启状态下,滑动距离小于阈值,则默认为不关闭,同时恢复原来的打开状态
  80. if (Math.abs(moveX) < state.threshold) {
  81. openSwipeAction(instance, ownerInstance)
  82. } else {
  83. // 如果滑动距离大于阈值,则执行收起逻辑
  84. closeSwipeAction(instance, ownerInstance)
  85. }
  86. } else {
  87. // 在关闭的状态下,右滑,无需操作
  88. if (moveX > 0) return
  89. // 理由同上
  90. if (Math.abs(moveX) < state.threshold) {
  91. closeSwipeAction(instance, ownerInstance)
  92. } else {
  93. openSwipeAction(instance, ownerInstance)
  94. }
  95. }
  96. }
  97. // 获取过渡时间
  98. function getDuration(value) {
  99. if (value.toString().indexOf('s') >= 0) return value
  100. return value > 30 ? value + 'ms' : value + 's'
  101. }
  102. // 滑动结束时判断滑动的方向
  103. function getMoveDirection(instance, ownerInstance) {
  104. var state = instance.getState()
  105. }
  106. // 移动滑动选择器内容区域,同时显示出其隐藏的菜单
  107. function moveSwipeAction(moveX, instance, ownerInstance) {
  108. var state = instance.getState()
  109. // 获取所有按钮的实例,需要通过它去设置按钮的位移
  110. var buttons = ownerInstance.selectAllComponents('.u-swipe-action-item__right__button')
  111. // 设置菜单内容部分的偏移
  112. instance.requestAnimationFrame(function() {
  113. instance.setStyle({
  114. // 设置translateX的值
  115. 'transition': 'none',
  116. transform: 'translateX(' + moveX + 'px)',
  117. '-webkit-transform': 'translateX(' + moveX + 'px)'
  118. })
  119. })
  120. }
  121. // 一次性展开滑动菜单
  122. function openSwipeAction(instance, ownerInstance) {
  123. var state = instance.getState()
  124. // 获取所有按钮的实例,需要通过它去设置按钮的位移
  125. var buttons = ownerInstance.selectAllComponents('.u-swipe-action-item__right__button')
  126. // 处理duration单位问题
  127. var duration = getDuration(state.duration)
  128. // 展开过程中,是向左移动,所以X的偏移应该为负值
  129. var buttonsWidth = -state.buttonsWidth
  130. instance.requestAnimationFrame(function() {
  131. // 设置菜单主体内容
  132. instance.setStyle({
  133. 'transition': 'transform ' + duration,
  134. 'transform': 'translateX(' + buttonsWidth + 'px)',
  135. '-webkit-transform': 'translateX(' + buttonsWidth + 'px)',
  136. })
  137. })
  138. setStatus('open', instance, ownerInstance)
  139. }
  140. // 标记菜单的当前状态,open-已经打开,close-已经关闭
  141. function setStatus(status, instance, ownerInstance) {
  142. var state = instance.getState()
  143. state.status = status
  144. ownerInstance.callMethod('setState', status)
  145. }
  146. // 一次性收起滑动菜单
  147. function closeSwipeAction(instance, ownerInstance) {
  148. var state = instance.getState()
  149. // 获取所有按钮的实例,需要通过它去设置按钮的位移
  150. var buttons = ownerInstance.selectAllComponents('.u-swipe-action-item__right__button')
  151. var len = buttons.length
  152. // 处理duration单位问题
  153. var duration = getDuration(state.duration)
  154. instance.requestAnimationFrame(function() {
  155. // 设置菜单主体内容
  156. instance.setStyle({
  157. 'transition': 'transform ' + duration,
  158. 'transform': 'translateX(0px)',
  159. '-webkit-transform': 'translateX(0px)'
  160. })
  161. // 设置各个隐藏的按钮为收起的状态
  162. for (var i = len - 1; i >= 0; i--) {
  163. buttons[i].setStyle({
  164. 'transition': 'transform ' + duration,
  165. 'transform': 'translateX(0px)',
  166. '-webkit-transform': 'translateX(0px)'
  167. })
  168. }
  169. })
  170. setStatus('close', instance, ownerInstance)
  171. }
  172. // status的状态发生变化
  173. function statusChange(newValue, oldValue, ownerInstance, instance) {
  174. var state = instance.getState()
  175. if (state.disabled) return
  176. // 打开或关闭单元格
  177. if (newValue === 'close' && state.status === 'open') {
  178. closeSwipeAction(instance, ownerInstance)
  179. } else if(newValue === 'open' && state.status === 'close') {
  180. openSwipeAction(instance, ownerInstance)
  181. }
  182. }
  183. // 菜单尺寸发生变化
  184. function sizeChange(newValue, oldValue, ownerInstance, instance) {
  185. // wxs内的局部变量快照
  186. var state = instance.getState()
  187. state.disabled = newValue.disabled
  188. state.duration = newValue.duration
  189. state.show = newValue.show
  190. state.threshold = newValue.threshold
  191. state.buttons = newValue.buttons
  192. if (state.buttons) {
  193. var len = state.buttons.length
  194. var buttonsWidth = 0
  195. var buttons = newValue.buttons
  196. for (var i = 0; i < len; i++) {
  197. buttonsWidth += buttons[i].width
  198. }
  199. }
  200. state.buttonsWidth = buttonsWidth
  201. }
  202. module.exports = {
  203. touchstart: touchstart,
  204. touchmove: touchmove,
  205. touchend: touchend,
  206. sizeChange: sizeChange,
  207. statusChange: statusChange
  208. }