You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
12 KiB

2 months ago
  1. // nvue操作dom的库,用于获取dom的尺寸信息
  2. const dom = uni.requireNativePlugin('dom')
  3. // nvue中用于操作元素动画的库,类似于uni.animation,只不过uni.animation不能用于nvue
  4. const animation = uni.requireNativePlugin('animation')
  5. import { sleep } from '../../libs/function/index';
  6. export default {
  7. data() {
  8. return {
  9. // 是否滑动中
  10. moving: false,
  11. // 状态,open-打开状态,close-关闭状态
  12. status: 'close',
  13. // 开始触摸点的X和Y轴坐标
  14. startX: 0,
  15. startY: 0,
  16. // 所有隐藏按钮的尺寸信息数组
  17. buttons: [],
  18. // 所有按钮的总宽度
  19. buttonsWidth: 0,
  20. // 记录上一次移动的位置值
  21. moveX: 0,
  22. // 记录上一次滑动的位置,用于前后两次做对比,如果移动的距离小于某一阈值,则认为前后之间没有移动,为了解决可能存在的通信阻塞问题
  23. lastX: 0
  24. }
  25. },
  26. computed: {
  27. // 获取过渡时间
  28. getDuratin() {
  29. let duration = String(this.duration)
  30. // 如果ms为单位,返回ms的数值部分
  31. if (duration.indexOf('ms') >= 0) return parseInt(duration)
  32. // 如果s为单位,为了得到ms的数值,需要乘以1000
  33. if (duration.indexOf('s') >= 0) return parseInt(duration) * 1000
  34. // 如果值传了数值,且小于30,认为是s单位
  35. duration = Number(duration)
  36. return duration < 30 ? duration * 1000 : duration
  37. }
  38. },
  39. watch: {
  40. show: {
  41. immediate: true,
  42. handler(n) {
  43. // if(n === true) {
  44. // sleep(50).then(() => {
  45. // this.openSwipeAction()
  46. // })
  47. // } else {
  48. // this.closeSwipeAction()
  49. // }
  50. }
  51. }
  52. },
  53. mounted() {
  54. sleep(20).then(() => {
  55. this.queryRect()
  56. })
  57. },
  58. methods: {
  59. close() {
  60. this.closeSwipeAction()
  61. },
  62. // 触摸单元格
  63. touchstart(event) {
  64. if (this.disabled) return
  65. this.closeOther()
  66. const { touches } = event
  67. // 记录触摸开始点的坐标值
  68. this.startX = touches[0].pageX
  69. this.startY = touches[0].pageY
  70. },
  71. // // 触摸滑动
  72. touchmove(event) {
  73. if (this.disabled) return
  74. const { touches } = event
  75. const { pageX } = touches[0]
  76. const { pageY } = touches[0]
  77. let moveX = pageX - this.startX
  78. const moveY = pageY - this.startY
  79. const { buttonsWidth } = this
  80. const len = this.buttons.length
  81. // 判断前后两次的移动距离,如果小于一定值,则不进行移动处理
  82. if (Math.abs(pageX - this.lastX) < 0.3) return
  83. this.lastX = pageX
  84. // 移动的X轴距离大于Y轴距离,也即终点与起点位置连线,与X轴夹角小于45度时,禁止页面滚动
  85. if (Math.abs(moveX) > Math.abs(moveY) || Math.abs(moveX) > this.threshold) {
  86. event.stopPropagation()
  87. }
  88. // 如果移动的X轴距离小于Y轴距离,也即终点位置与起点位置连线,与Y轴夹角小于45度时,认为是页面上下滑动,而不是左右滑动单元格
  89. if (Math.abs(moveX) < Math.abs(moveY)) return
  90. // 限制右滑的距离,不允许内容部分往右偏移,右滑会导致X轴偏移值大于0,以此做判断
  91. // 此处不能直接return,因为滑动过程中会缺失某些关键点坐标,会导致错乱,最好的办法就是
  92. // 在超出后,设置为0
  93. if (this.status === 'open') {
  94. // 在开启状态下,向左滑动,需忽略
  95. if (moveX < 0) moveX = 0
  96. // 想要收起菜单,最大能移动的距离为按钮的总宽度
  97. if (moveX > buttonsWidth) moveX = buttonsWidth
  98. // 如果是已经打开了的状态,向左滑动时,移动收起菜单
  99. this.moveSwipeAction(-buttonsWidth + moveX)
  100. } else {
  101. // 关闭状态下,右滑动需忽略
  102. if (moveX > 0) moveX = 0
  103. // 滑动的距离不允许超过所有按钮的总宽度,此时只能是左滑,最终设置按钮的总宽度,同时为负数
  104. if (Math.abs(moveX) > buttonsWidth) moveX = -buttonsWidth
  105. // 只要是在滑过程中,就不断移动菜单的内容部分,从而使隐藏的菜单显示出来
  106. this.moveSwipeAction(moveX)
  107. }
  108. },
  109. // 单元格结束触摸
  110. touchend(event) {
  111. if (this.disabled) return
  112. const touches = event.changedTouches ? event.changedTouches[0] : {}
  113. const { pageX } = touches
  114. const { pageY } = touches
  115. const { buttonsWidth } = this
  116. this.moveX = pageX - this.startX
  117. if (this.status === 'open') {
  118. // 在展开的状态下,继续左滑,无需操作
  119. if (this.moveX < 0) this.moveX = 0
  120. if (this.moveX > buttonsWidth) this.moveX = buttonsWidth
  121. // 在开启状态下,点击一下内容区域,moveX为0,也即没有进行移动,这时执行收起菜单逻辑
  122. if (this.moveX === 0) {
  123. return this.closeSwipeAction()
  124. }
  125. // 在开启状态下,滑动距离小于阈值,则默认为不关闭,同时恢复原来的打开状态
  126. if (Math.abs(this.moveX) < this.threshold) {
  127. this.openSwipeAction()
  128. } else {
  129. // 如果滑动距离大于阈值,则执行收起逻辑
  130. this.closeSwipeAction()
  131. }
  132. } else {
  133. // 在关闭的状态下,右滑,无需操作
  134. if (this.moveX >= 0) this.moveX = 0
  135. if (this.moveX <= -buttonsWidth) this.moveX = -buttonsWidth
  136. // 理由同上
  137. if (Math.abs(this.moveX) < this.threshold) {
  138. this.closeSwipeAction()
  139. } else {
  140. this.openSwipeAction()
  141. }
  142. }
  143. },
  144. // 移动滑动选择器内容区域,同时显示出其隐藏的菜单
  145. moveSwipeAction(moveX) {
  146. if (this.moving) return
  147. this.moving = true
  148. let previewButtonsMoveX = 0
  149. const len = this.buttons.length
  150. animation.transition(this.$refs['u-swipe-action-item__content'].ref, {
  151. styles: {
  152. transform: `translateX(${moveX}px)`
  153. },
  154. timingFunction: 'linear'
  155. }, () => {
  156. this.moving = false
  157. })
  158. // 按钮的组的长度
  159. for (let i = len - 1; i >= 0; i--) {
  160. const buttonRef = this.$refs[`u-swipe-action-item__right__button-${i}`][0].ref
  161. // 通过比例,得出元素自身该移动的距离
  162. const translateX = this.buttons[i].width / this.buttonsWidth * moveX
  163. // 最终移动的距离,是通过自身比例算出的距离,再加上在它之前所有按钮移动的距离之和
  164. const realTranslateX = translateX + previewButtonsMoveX
  165. animation.transition(buttonRef, {
  166. styles: {
  167. transform: `translateX(${realTranslateX}px)`
  168. },
  169. duration: 0,
  170. delay: 0,
  171. timingFunction: 'linear'
  172. }, () => {})
  173. // 记录本按钮之前的所有按钮的移动距离之和
  174. previewButtonsMoveX += translateX
  175. }
  176. },
  177. // 关闭菜单
  178. closeSwipeAction() {
  179. if (this.status === 'close') return
  180. this.moving = true
  181. const { buttonsWidth } = this
  182. animation.transition(this.$refs['u-swipe-action-item__content'].ref, {
  183. styles: {
  184. transform: 'translateX(0px)'
  185. },
  186. duration: this.getDuratin,
  187. timingFunction: 'ease-in-out'
  188. }, () => {
  189. this.status = 'close'
  190. this.moving = false
  191. this.closeHandler()
  192. })
  193. // 按钮的组的长度
  194. const len = this.buttons.length
  195. for (let i = len - 1; i >= 0; i--) {
  196. const buttonRef = this.$refs[`u-swipe-action-item__right__button-${i}`][0].ref
  197. // 如果不满足边界条件,返回
  198. if (this.buttons.length === 0 || !this.buttons[i] || !this.buttons[i].width) return
  199. animation.transition(buttonRef, {
  200. styles: {
  201. transform: 'translateX(0px)'
  202. },
  203. duration: this.getDuratin,
  204. timingFunction: 'ease-in-out'
  205. }, () => {})
  206. }
  207. },
  208. // 打开菜单
  209. openSwipeAction() {
  210. if (this.status === 'open') return
  211. this.moving = true
  212. const buttonsWidth = -this.buttonsWidth
  213. let previewButtonsMoveX = 0
  214. animation.transition(this.$refs['u-swipe-action-item__content'].ref, {
  215. styles: {
  216. transform: `translateX(${buttonsWidth}px)`
  217. },
  218. duration: this.getDuratin,
  219. timingFunction: 'ease-in-out'
  220. }, () => {
  221. this.status = 'open'
  222. this.moving = false
  223. this.openHandler()
  224. })
  225. // 按钮的组的长度
  226. const len = this.buttons.length
  227. for (let i = len - 1; i >= 0; i--) {
  228. const buttonRef = this.$refs[`u-swipe-action-item__right__button-${i}`][0].ref
  229. // 如果不满足边界条件,返回
  230. if (this.buttons.length === 0 || !this.buttons[i] || !this.buttons[i].width) return
  231. // 通过比例,得出元素自身该移动的距离
  232. const translateX = this.buttons[i].width / this.buttonsWidth * buttonsWidth
  233. // 最终移动的距离,是通过自身比例算出的距离,再加上在它之前所有按钮移动的距离之和
  234. const realTranslateX = translateX + previewButtonsMoveX
  235. animation.transition(buttonRef, {
  236. styles: {
  237. transform: `translateX(${realTranslateX}px)`
  238. },
  239. duration: this.getDuratin,
  240. timingFunction: 'ease-in-out'
  241. }, () => {})
  242. previewButtonsMoveX += translateX
  243. }
  244. },
  245. // 查询按钮节点信息
  246. queryRect() {
  247. // 历遍所有按钮数组,通过getRectByDom返回一个promise
  248. const promiseAll = this.rightOptions.map((item, index) => this.getRectByDom(this.$refs[`u-swipe-action-item__right__button-${index}`][0]))
  249. // 通过promise.all方法,让所有按钮的查询结果返回一个数组的形式
  250. Promise.all(promiseAll).then((sizes) => {
  251. this.buttons = sizes
  252. // 计算所有按钮总宽度
  253. this.buttonsWidth = sizes.reduce((sum, cur) => sum + cur.width, 0)
  254. })
  255. },
  256. // 通过nvue的dom模块,查询节点信息
  257. getRectByDom(ref) {
  258. return new Promise((resolve) => {
  259. dom.getComponentRect(ref, (res) => {
  260. resolve(res.size)
  261. })
  262. })
  263. }
  264. }
  265. }