mpalipay.js 4.2 KB

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