index.vue 7.7 KB

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