tui-swipe-action.vue 6.6 KB

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