swipe-action.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="tui-swipeout-wrap">
  3. <view class="tui-swipeout-item" :class="[isShowBtn?'swipe-action-show':'']" @touchstart="handlerTouchstart"
  4. @touchmove="handlerTouchmove" @touchend="handlerTouchend" :style="{transform:'translate(' + position.pageX + 'px,0)'}">
  5. <view class="tui-swipeout-content">
  6. <slot name="content"></slot>
  7. </view>
  8. <view class="tui-swipeout-button-right-group" v-if="actions.length > 0" @touchend.stop="loop">
  9. <view class="tui-swipeout-button-right-item" v-for="(item,index) in actions" :key="index" :style="{background:item.background || '#f7f7f7',color:item.color,width:item.width+'px'}"
  10. :data-index="index" @tap="handlerButton">
  11. <image :src="item.icon" v-if="item.icon" :style="{width:px(item.imgWidth),height:px(item.imgHeight)}"></image>
  12. <text :style="{fontSize:px(item.fontsize)}">{{item.name}}</text>
  13. </view>
  14. </view>
  15. <!--actions长度设置为0,可直接传按钮进来-->
  16. <view class="tui-swipeout-button-right-group" @touchend.stop="loop" @tap="handlerParentButton" v-if="actions.length === 0"
  17. :style="{width:operateWidth+'px',right:'-'+operateWidth+'px'}">
  18. <slot name="button"></slot>
  19. </view>
  20. </view>
  21. <view v-if="isShowBtn && showMask" class="swipe-action_mask" @tap="closeButtonGroup" @touchmove.stop="closeButtonGroup" />
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. name: "tuiSwipeAction",
  27. props: {
  28. // name: '删除',
  29. // color: '#fff',
  30. // fontsize: 32,//单位upx
  31. // width: 80, //单位px
  32. // icon: 'like.png',//此处为图片地址
  33. // background: '#ed3f14'
  34. actions: {
  35. type: Array,
  36. default: function(){
  37. return []
  38. }
  39. },
  40. //是否可关闭,默认可关闭,可以单独控制
  41. closable: {
  42. type: Boolean,
  43. default: true
  44. },
  45. //设为false,可以滑动多行不关闭菜单
  46. showMask: {
  47. type: Boolean,
  48. default: true
  49. },
  50. operateWidth: {
  51. type: Number,
  52. default: 80
  53. },
  54. params: {
  55. type: Object,
  56. default: function(){
  57. return {}
  58. }
  59. }
  60. },
  61. watch: {
  62. actions(newValue, oldValue) {
  63. this.updateButtonSize()
  64. }
  65. },
  66. data() {
  67. return {
  68. //start position
  69. tStart: {
  70. pageX: 0,
  71. pageY: 0
  72. },
  73. //限制滑动距离
  74. limitMove: 0,
  75. //move position
  76. position: {
  77. pageX: 0,
  78. pageY: 0
  79. },
  80. isShowBtn: false
  81. };
  82. },
  83. // #ifdef H5
  84. mounted() {
  85. this.updateButtonSize()
  86. },
  87. // #endif
  88. onReady() {
  89. this.updateButtonSize()
  90. },
  91. methods: {
  92. swipeDirection(x1, x2, y1, y2) {
  93. return Math.abs(x1 - x2) >=
  94. Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  95. },
  96. //阻止事件冒泡
  97. loop() {},
  98. updateButtonSize() {
  99. const actions = this.actions;
  100. if (actions.length > 0) {
  101. const query = uni.createSelectorQuery().in(this);
  102. let limitMovePosition = 0;
  103. actions.forEach(item => {
  104. limitMovePosition += item.width || 0;
  105. });
  106. this.limitMove = limitMovePosition;
  107. } else {
  108. this.limitMove = this.operateWidth;
  109. }
  110. },
  111. handlerTouchstart(event) {
  112. const touches = event.touches ? event.touches[0] : {};
  113. const tStart = this.tStart;
  114. if (touches) {
  115. for (let i in tStart) {
  116. if (touches[i]) {
  117. tStart[i] = touches[i];
  118. }
  119. }
  120. }
  121. },
  122. swipper(touches) {
  123. const start = this.tStart;
  124. const spacing = {
  125. pageX: touches.pageX - start.pageX,
  126. pageY: touches.pageY - start.pageY
  127. }
  128. if (this.limitMove < Math.abs(spacing.pageX)) {
  129. spacing.pageX = -this.limitMove;
  130. }
  131. this.position = spacing
  132. },
  133. handlerTouchmove(event) {
  134. const start = this.tStart;
  135. const touches = event.touches ? event.touches[0] : {};
  136. if (touches) {
  137. const direction = this.swipeDirection(start.pageX, touches.pageX, start.pageY, touches.pageY);
  138. if (direction === 'Left') {
  139. this.swipper(touches);
  140. }
  141. }
  142. },
  143. handlerTouchend(event) {
  144. const start = this.tStart;
  145. const touches = event.changedTouches ? event.changedTouches[0] : {};
  146. if (touches) {
  147. const direction = this.swipeDirection(start.pageX, touches.pageX, start.pageY, touches.pageY);
  148. const spacing = {
  149. pageX: touches.pageX - start.pageX,
  150. pageY: touches.pageY - start.pageY
  151. }
  152. if (Math.abs(spacing.pageX) >= 40 && direction === "Left") {
  153. spacing.pageX = spacing.pageX < 0 ? -this.limitMove : this.limitMove;
  154. this.isShowBtn = true
  155. } else {
  156. spacing.pageX = 0;
  157. }
  158. this.position = spacing
  159. }
  160. },
  161. handlerButton(event) {
  162. if (this.closable) {
  163. this.closeButtonGroup();
  164. }
  165. const dataset = event.currentTarget.dataset;
  166. this.$emit('click', {
  167. index: Number(dataset.index),
  168. item: this.params
  169. })
  170. },
  171. closeButtonGroup() {
  172. this.position = {
  173. pageX: 0,
  174. pageY: 0
  175. };
  176. this.isShowBtn = false
  177. },
  178. //控制自定义组件
  179. handlerParentButton(event) {
  180. if (this.closable) {
  181. this.closeButtonGroup();
  182. }
  183. },
  184. px(num) {
  185. return uni.upx2px(num) + "px"
  186. }
  187. }
  188. }
  189. </script>
  190. <style>
  191. .tui-swipeout-wrap {
  192. background: #fff;
  193. position: relative;
  194. overflow: hidden;
  195. }
  196. .swipe-action-show {
  197. position: relative;
  198. z-index: 99999
  199. }
  200. .tui-swipeout-item {
  201. width: 100%;
  202. /* padding: 15px 20px; */
  203. box-sizing: border-box;
  204. transition: transform 0.2s ease;
  205. font-size: 14px;
  206. }
  207. .tui-swipeout-content {
  208. white-space: nowrap;
  209. overflow: hidden;
  210. }
  211. .tui-swipeout-button-right-group {
  212. position: absolute;
  213. right: -100%;
  214. top: 0;
  215. height: 100%;
  216. z-index: 1;
  217. width: 100%;
  218. }
  219. .tui-swipeout-button-right-item {
  220. height: 100%;
  221. float: left;
  222. white-space: nowrap;
  223. box-sizing: border-box;
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. text-align: center;
  228. }
  229. .swipe-action_mask {
  230. display: block;
  231. opacity: 0;
  232. position: fixed;
  233. z-index: 999;
  234. top: 0;
  235. left: 0;
  236. width: 100%;
  237. height: 100%;
  238. }
  239. </style>