mpalipay.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. export default {
  2. data() {
  3. return {
  4. x: 0,
  5. transition: false,
  6. width: 0,
  7. viewWidth: 0,
  8. swipeShow: 0
  9. }
  10. },
  11. watch: {
  12. show(newVal) {
  13. if (this.autoClose) return
  14. if (newVal && newVal !== 'none') {
  15. this.transition = true
  16. this.open(newVal)
  17. } else {
  18. this.close()
  19. }
  20. }
  21. },
  22. created() {
  23. this.swipeaction = this.getSwipeAction()
  24. if (this.swipeaction && Array.isArray(this.swipeaction.children)) {
  25. this.swipeaction.children.push(this)
  26. }
  27. },
  28. mounted() {
  29. this.isopen = false
  30. setTimeout(() => {
  31. this.getQuerySelect()
  32. }, 50)
  33. },
  34. methods: {
  35. appTouchStart(e) {
  36. const {
  37. clientX
  38. } = e.changedTouches[0]
  39. this.clientX = clientX
  40. this.timestamp = new Date().getTime()
  41. },
  42. appTouchEnd(e, index, item, position) {
  43. const {
  44. clientX
  45. } = e.changedTouches[0]
  46. // fixed by xxxx 模拟点击事件,解决 ios 13 点击区域错位的问题
  47. let diff = Math.abs(this.clientX - clientX)
  48. let time = (new Date().getTime()) - this.timestamp
  49. if (diff < 40 && time < 300) {
  50. this.$emit('click', {
  51. content: item,
  52. index,
  53. position
  54. })
  55. }
  56. },
  57. /**
  58. * 移动触发
  59. * @param {Object} e
  60. */
  61. onChange(e) {
  62. this.moveX = e.detail.x
  63. this.isclose = false
  64. },
  65. touchstart(e) {
  66. this.transition = false
  67. this.isclose = true
  68. if (this.autoClose && this.swipeaction) {
  69. this.swipeaction.closeOther(this)
  70. }
  71. },
  72. touchmove(e) {},
  73. touchend(e) {
  74. // 0的位置什么都不执行
  75. if (this.isclose && this.isopen === 'none') return
  76. if (this.isclose && this.isopen !== 'none') {
  77. this.transition = true
  78. this.close()
  79. } else {
  80. this.move(this.moveX + this.leftWidth)
  81. }
  82. },
  83. /**
  84. * 移动
  85. * @param {Object} moveX
  86. */
  87. move(moveX) {
  88. // 打开关闭的处理逻辑不太一样
  89. this.transition = true
  90. // 未打开状态
  91. if (!this.isopen || this.isopen === 'none') {
  92. if (moveX > this.threshold) {
  93. this.open('left')
  94. } else if (moveX < -this.threshold) {
  95. this.open('right')
  96. } else {
  97. this.close()
  98. }
  99. } else {
  100. if (moveX < 0 && moveX < this.rightWidth) {
  101. const rightX = this.rightWidth + moveX
  102. if (rightX < this.threshold) {
  103. this.open('right')
  104. } else {
  105. this.close()
  106. }
  107. } else if (moveX > 0 && moveX < this.leftWidth) {
  108. const leftX = this.leftWidth - moveX
  109. if (leftX < this.threshold) {
  110. this.open('left')
  111. } else {
  112. this.close()
  113. }
  114. }
  115. }
  116. },
  117. /**
  118. * 打开
  119. */
  120. open(type) {
  121. this.x = this.moveX
  122. this.animation(type)
  123. },
  124. /**
  125. * 关闭
  126. */
  127. close() {
  128. this.x = this.moveX
  129. // TODO 解决 x 值不更新的问题,所以会多触发一次 nextTick ,待优化
  130. this.$nextTick(() => {
  131. this.x = -this.leftWidth
  132. if (this.isopen !== 'none') {
  133. this.$emit('change', 'none')
  134. }
  135. this.isopen = 'none'
  136. })
  137. },
  138. /**
  139. * 执行结束动画
  140. * @param {Object} type
  141. */
  142. animation(type) {
  143. this.$nextTick(() => {
  144. if (type === 'left') {
  145. this.x = 0
  146. } else {
  147. this.x = -this.rightWidth - this.leftWidth
  148. }
  149. if (this.isopen !== type) {
  150. this.$emit('change', type)
  151. }
  152. this.isopen = type
  153. })
  154. },
  155. getSlide(x) {},
  156. getQuerySelect() {
  157. const query = uni.createSelectorQuery().in(this);
  158. query.selectAll('.movable-view--hock').boundingClientRect(data => {
  159. this.leftWidth = data[1].width
  160. this.rightWidth = data[2].width
  161. this.width = data[0].width
  162. this.viewWidth = this.width + this.rightWidth + this.leftWidth
  163. if (this.leftWidth === 0) {
  164. // TODO 疑似bug ,初始化的时候如果x 是0,会导致移动位置错误,所以让元素超出一点
  165. this.x = -0.1
  166. } else {
  167. this.x = -this.leftWidth
  168. }
  169. this.moveX = this.x
  170. this.$nextTick(() => {
  171. this.swipeShow = 1
  172. })
  173. if (!this.buttonWidth) {
  174. this.disabledView = true
  175. }
  176. if (this.autoClose) return
  177. if (this.show !== 'none') {
  178. this.transition = true
  179. this.open(this.shows)
  180. }
  181. }).exec();
  182. }
  183. }
  184. }