mpalipay.js 5.6 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.children !== undefined) {
  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. this.autoClose && this.swipeaction.closeOther(this)
  69. },
  70. touchmove(e) {
  71. },
  72. touchend(e) {
  73. // 0的位置什么都不执行
  74. if (this.isclose && this.isopen === 'none') return
  75. if (this.isclose && this.isopen !== 'none') {
  76. this.transition = true
  77. this.close()
  78. } else {
  79. this.move(this.moveX + this.leftWidth)
  80. }
  81. },
  82. /**
  83. * 移动
  84. * @param {Object} moveX
  85. */
  86. move(moveX) {
  87. // 打开关闭的处理逻辑不太一样
  88. this.transition = true
  89. // 未打开状态
  90. if (!this.isopen || this.isopen === 'none') {
  91. if (moveX > this.threshold) {
  92. this.open('left')
  93. } else if (moveX < -this.threshold) {
  94. this.open('right')
  95. } else {
  96. this.close()
  97. }
  98. } else {
  99. if (moveX < 0 && moveX < this.rightWidth) {
  100. const rightX = this.rightWidth + moveX
  101. if (rightX < this.threshold) {
  102. this.open('right')
  103. } else {
  104. this.close()
  105. }
  106. } else if (moveX > 0 && moveX < this.leftWidth) {
  107. const leftX = this.leftWidth - moveX
  108. if (leftX < this.threshold) {
  109. this.open('left')
  110. } else {
  111. this.close()
  112. }
  113. }
  114. }
  115. },
  116. /**
  117. * 打开
  118. */
  119. open(type) {
  120. this.x = this.moveX
  121. this.animation(type)
  122. },
  123. /**
  124. * 关闭
  125. */
  126. close() {
  127. this.x = this.moveX
  128. // TODO 解决 x 值不更新的问题,所以会多触发一次 nextTick ,待优化
  129. this.$nextTick(() => {
  130. this.x = -this.leftWidth
  131. if (this.isopen !== 'none') {
  132. this.$emit('change', 'none')
  133. }
  134. this.isopen = 'none'
  135. })
  136. },
  137. /**
  138. * 执行结束动画
  139. * @param {Object} type
  140. */
  141. animation(type) {
  142. this.$nextTick(() => {
  143. if (type === 'left') {
  144. this.x = 0
  145. } else {
  146. this.x = -this.rightWidth - this.leftWidth
  147. }
  148. if (this.isopen !== type) {
  149. this.$emit('change', type)
  150. }
  151. this.isopen = type
  152. })
  153. },
  154. getSlide(x) {
  155. },
  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. }