tui-swipe-action.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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="{ backgroundColor: 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.stop="closeButtonGroup" @touchstart.stop.prevent="closeButtonGroup" />
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. name: 'tuiSwipeAction',
  27. props: {
  28. // name: '删除',
  29. // color: '#fff',
  30. // fontsize: 32,//单位rpx
  31. // width: 80, //单位px
  32. // icon: 'like.png',//此处为图片地址
  33. // background: '#ed3f14'
  34. actions: {
  35. type: Array,
  36. default () {
  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 () {
  57. return {};
  58. }
  59. },
  60. //禁止滑动
  61. forbid: {
  62. type: Boolean,
  63. default: false
  64. },
  65. //手动开关
  66. open: {
  67. type: Boolean,
  68. default: false
  69. }
  70. },
  71. watch: {
  72. actions(newValue, oldValue) {
  73. this.updateButtonSize();
  74. },
  75. open(newValue) {
  76. this.manualSwitch(newValue);
  77. }
  78. },
  79. data() {
  80. return {
  81. //start position
  82. tStart: {
  83. pageX: 0,
  84. pageY: 0
  85. },
  86. //限制滑动距离
  87. limitMove: 0,
  88. //move position
  89. position: {
  90. pageX: 0,
  91. pageY: 0
  92. },
  93. isShowBtn: false
  94. };
  95. },
  96. mounted() {
  97. this.updateButtonSize();
  98. },
  99. methods: {
  100. swipeDirection(x1, x2, y1, y2) {
  101. return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : y1 - y2 > 0 ? 'Up' : 'Down';
  102. },
  103. //阻止事件冒泡
  104. loop() {},
  105. updateButtonSize() {
  106. const actions = this.actions;
  107. if (actions.length > 0) {
  108. const query = uni.createSelectorQuery().in(this);
  109. let limitMovePosition = 0;
  110. actions.forEach(item => {
  111. limitMovePosition += item.width || 0;
  112. });
  113. this.limitMove = limitMovePosition;
  114. } else {
  115. this.limitMove = this.operateWidth;
  116. }
  117. },
  118. handlerTouchstart(event) {
  119. if (this.forbid) return;
  120. const touches = event.touches ? event.touches[0] : {};
  121. const tStart = this.tStart;
  122. if (touches) {
  123. for (let i in tStart) {
  124. if (touches[i]) {
  125. tStart[i] = touches[i];
  126. }
  127. }
  128. }
  129. },
  130. swipper(touches) {
  131. const start = this.tStart;
  132. const spacing = {
  133. pageX: touches.pageX - start.pageX,
  134. pageY: touches.pageY - start.pageY
  135. };
  136. if (this.limitMove < Math.abs(spacing.pageX)) {
  137. spacing.pageX = -this.limitMove;
  138. }
  139. this.position = spacing;
  140. },
  141. handlerTouchmove(event) {
  142. if (this.forbid) return;
  143. const start = this.tStart;
  144. const touches = event.touches ? event.touches[0] : {};
  145. if (touches) {
  146. const direction = this.swipeDirection(start.pageX, touches.pageX, start.pageY, touches.pageY);
  147. if (direction === 'Left' && Math.abs(this.position.pageX) !== this.limitMove) {
  148. this.swipper(touches);
  149. }
  150. }
  151. },
  152. handlerTouchend(event) {
  153. if (this.forbid) return;
  154. const start = this.tStart;
  155. const touches = event.changedTouches ? event.changedTouches[0] : {};
  156. if (touches) {
  157. const direction = this.swipeDirection(start.pageX, touches.pageX, start.pageY, touches.pageY);
  158. const spacing = {
  159. pageX: touches.pageX - start.pageX,
  160. pageY: touches.pageY - start.pageY
  161. };
  162. if (Math.abs(spacing.pageX) >= 40 && direction === 'Left') {
  163. spacing.pageX = spacing.pageX < 0 ? -this.limitMove : this.limitMove;
  164. this.isShowBtn = true;
  165. } else {
  166. spacing.pageX = 0;
  167. }
  168. this.position = spacing;
  169. }
  170. },
  171. handlerButton(event) {
  172. if (this.closable) {
  173. this.closeButtonGroup();
  174. }
  175. const dataset = event.currentTarget.dataset;
  176. this.$emit('click', {
  177. index: Number(dataset.index),
  178. item: this.params
  179. });
  180. },
  181. closeButtonGroup() {
  182. this.position = {
  183. pageX: 0,
  184. pageY: 0
  185. };
  186. this.isShowBtn = false;
  187. },
  188. //控制自定义按钮菜单
  189. handlerParentButton(event) {
  190. if (this.closable) {
  191. this.closeButtonGroup();
  192. }
  193. },
  194. manualSwitch(isOpen) {
  195. let x = 0;
  196. if (isOpen) {
  197. if (this.actions.length === 0) {
  198. x = this.operateWidth;
  199. } else {
  200. let width = 0;
  201. this.actions.forEach(item => {
  202. width += item.width;
  203. });
  204. x = width;
  205. }
  206. }
  207. this.position = {
  208. pageX: -x,
  209. pageY: 0
  210. };
  211. },
  212. px(num) {
  213. return uni.upx2px(num) + 'px';
  214. }
  215. }
  216. };
  217. </script>
  218. <style scoped>
  219. .tui-swipeout-wrap {
  220. background-color: #fff;
  221. position: relative;
  222. overflow: hidden;
  223. }
  224. .swipe-action-show {
  225. position: relative;
  226. z-index: 998;
  227. }
  228. .tui-swipeout-item {
  229. width: 100%;
  230. /* padding: 15px 20px; */
  231. box-sizing: border-box;
  232. transition: transform 0.2s ease;
  233. font-size: 14px;
  234. }
  235. .tui-swipeout-content {
  236. white-space: nowrap;
  237. overflow: hidden;
  238. }
  239. .tui-swipeout-button-right-group {
  240. position: absolute;
  241. right: -100%;
  242. top: 0;
  243. height: 100%;
  244. z-index: 1;
  245. width: 100%;
  246. }
  247. .tui-swipeout-button-right-item {
  248. height: 100%;
  249. float: left;
  250. white-space: nowrap;
  251. box-sizing: border-box;
  252. display: flex;
  253. align-items: center;
  254. justify-content: center;
  255. text-align: center;
  256. }
  257. .swipe-action_mask {
  258. display: block;
  259. opacity: 0;
  260. position: fixed;
  261. z-index: 997;
  262. top: 0;
  263. left: 0;
  264. width: 100%;
  265. height: 100%;
  266. }
  267. </style>