nvue - backup.js 12 KB

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