swipe-action.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * touch事件判断方式
  3. */
  4. function swipeDirection(x1, x2, y1, y2) {
  5. return Math.abs(x1 - x2) >=
  6. Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  7. }
  8. Component({
  9. properties: {
  10. actions: {
  11. value: [],
  12. type: Array,
  13. observer: 'updateButtonSize'
  14. },
  15. unclosable: {
  16. value: false,
  17. type: Boolean
  18. },
  19. toggle: {
  20. value: false,
  21. type: Boolean,
  22. observer: 'closeButtonGroup'
  23. },
  24. operateWidth: {
  25. type: Number,
  26. value: 160
  27. },
  28. params:{
  29. type:Object,
  30. value:{}
  31. }
  32. },
  33. options: {
  34. // 在组件定义时的选项中启用多slot支持
  35. multipleSlots: true
  36. },
  37. data: {
  38. //touch start position
  39. tStart: {
  40. pageX: 0,
  41. pageY: 0
  42. },
  43. //限制滑动距离
  44. limitMove: 0,
  45. //element move position
  46. position: {
  47. pageX: 0,
  48. pageY: 0
  49. },
  50. isShowBtn:false,
  51. isShow:false
  52. },
  53. methods: {
  54. //阻止事件冒泡
  55. loop() { },
  56. updateButtonSize() {
  57. const actions = this.data.actions;
  58. if (actions.length > 0) {
  59. const query = wx.createSelectorQuery().in(this);
  60. let limitMovePosition = 0;
  61. actions.forEach(item => {
  62. limitMovePosition += item.width || 0;
  63. });
  64. this.data.limitMove = limitMovePosition;
  65. } else {
  66. this.data.limitMove = this.data.operateWidth;
  67. }
  68. },
  69. handlerTouchstart(event) {
  70. const touches = event.touches ? event.touches[0] : {};
  71. const tStart = this.data.tStart;
  72. if (touches) {
  73. for (let i in tStart) {
  74. if (touches[i]) {
  75. tStart[i] = touches[i];
  76. }
  77. }
  78. }
  79. },
  80. swipper(touches) {
  81. const data = this.data;
  82. const start = data.tStart;
  83. const spacing = {
  84. pageX: touches.pageX - start.pageX,
  85. pageY: touches.pageY - start.pageY
  86. }
  87. if (data.limitMove < Math.abs(spacing.pageX)) {
  88. spacing.pageX = -data.limitMove;
  89. }
  90. this.setData({
  91. 'position': spacing
  92. })
  93. },
  94. handlerTouchmove(event) {
  95. const start = this.data.tStart;
  96. const touches = event.touches ? event.touches[0] : {};
  97. if (touches) {
  98. const direction = swipeDirection(start.pageX, touches.pageX, start.pageY, touches.pageY);
  99. if (direction === 'Left') {
  100. this.swipper(touches);
  101. }
  102. }
  103. },
  104. handlerTouchend(event) {
  105. const start = this.data.tStart;
  106. const touches = event.changedTouches ? event.changedTouches[0] : {};
  107. if (touches) {
  108. const direction = swipeDirection(start.pageX, touches.pageX, start.pageY, touches.pageY);
  109. const spacing = {
  110. pageX: touches.pageX - start.pageX,
  111. pageY: touches.pageY - start.pageY
  112. }
  113. if (Math.abs(spacing.pageX) >= 40 && direction === "Left") {
  114. spacing.pageX = spacing.pageX < 0 ? - this.data.limitMove : this.data.limitMove;
  115. this.setData({
  116. isShowBtn: true,
  117. isShow:true
  118. })
  119. this.triggerEvent('click',true)
  120. } else {
  121. if(!this.data.isShow){
  122. this.triggerEvent('click',false)
  123. }
  124. this.data.isShow = false;
  125. spacing.pageX = 0;
  126. }
  127. console.log(spacing)
  128. this.setData({
  129. 'position': spacing
  130. })
  131. }
  132. },
  133. handlerButton(event) {
  134. if (!this.data.unclosable) {
  135. this.closeButtonGroup();
  136. }
  137. const dataset = event.currentTarget.dataset;
  138. this.triggerEvent('change', {
  139. index: dataset.index,
  140. item:this.data.params
  141. })
  142. },
  143. closeButtonGroup() {
  144. this.setData({
  145. 'position': { pageX: 0, pageY: 0 },
  146. isShowBtn:false,
  147. isShow:false
  148. })
  149. this.triggerEvent('click',false)
  150. },
  151. //控制自定义组件
  152. handlerParentButton(event) {
  153. if (!this.data.unclosable) {
  154. this.closeButtonGroup();
  155. }
  156. }
  157. },
  158. ready() {
  159. this.updateButtonSize();
  160. }
  161. });