tui-swipe-action.vue 7.0 KB

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