u-swipe-action.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <movable-area class="u-swipe-action" :style="{ backgroundColor: bgColor }">
  3. <movable-view
  4. class="u-swipe-view"
  5. @change="change"
  6. @touchend="touchend"
  7. @touchstart="touchstart"
  8. direction="horizontal"
  9. :disabled="disabled"
  10. :x="moveX"
  11. :style="{
  12. width: movableViewWidth ? movableViewWidth : '100%'
  13. }"
  14. >
  15. <view
  16. class="u-swipe-content"
  17. @tap.stop="contentClick"
  18. >
  19. <slot></slot>
  20. </view>
  21. <view class="u-swipe-del" v-if="showBtn" @tap.stop="btnClick(index)" :style="[btnStyle(item.style)]" v-for="(item, index) in options" :key="index">
  22. <view class="u-btn-text">{{ item.text }}</view>
  23. </view>
  24. </movable-view>
  25. </movable-area>
  26. </template>
  27. <script>
  28. /**
  29. * swipeAction 左滑单元格
  30. * @description 该组件一般用于左滑唤出操作菜单的场景,用的最多的是左滑删除操作。
  31. * @tutorial https://www.uviewui.com/components/swipeAction.html
  32. * @property {String} bg-color 整个组件背景颜色(默认#ffffff)
  33. * @property {Array} options 数组形式,可以配置背景颜色和文字
  34. * @property {String Number} index 标识符,点击时候用于区分点击了哪一个,用v-for循环时的index即可
  35. * @property {String Number} btn-width 按钮宽度,单位rpx(默认180)
  36. * @property {Boolean} disabled 是否禁止某个swipeAction滑动(默认false)
  37. * @property {Boolean} show 打开或者关闭某个组件(默认false)
  38. * @event {Function} click 点击组件时触发
  39. * @event {Function} close 组件触发关闭状态时
  40. * @event {Function} content-click 点击内容时触发
  41. * @event {Function} open 组件触发打开状态时
  42. * @example <u-swipe-action btn-text="收藏">...</u-swipe-action>
  43. */
  44. export default {
  45. name: 'u-swipe-action',
  46. props: {
  47. // index值,用于得知点击删除的是哪个按钮
  48. index: {
  49. type: [Number, String],
  50. default: ''
  51. },
  52. // 滑动按钮的宽度,单位为rpx
  53. btnWidth: {
  54. type: [String, Number],
  55. default: 180
  56. },
  57. // 是否禁止某个action滑动
  58. disabled: {
  59. type: Boolean,
  60. default: false
  61. },
  62. // 打开或者关闭组件
  63. show: {
  64. type: Boolean,
  65. default: false
  66. },
  67. // 组件背景颜色
  68. bgColor: {
  69. type: String,
  70. default: '#ffffff'
  71. },
  72. // 是否使手机发生短促震动,目前只在iOS的微信小程序有效(2020-05-06)
  73. vibrateShort: {
  74. type: Boolean,
  75. default: false
  76. },
  77. // 按钮操作参数
  78. options: {
  79. type: Array,
  80. default() {
  81. return [];
  82. }
  83. }
  84. },
  85. watch: {
  86. show: {
  87. immediate: true,
  88. handler(nVal, oVal) {
  89. if (nVal) {
  90. this.open();
  91. } else {
  92. this.close();
  93. }
  94. }
  95. }
  96. },
  97. data() {
  98. return {
  99. moveX: 0, // movable-view元素在x轴上需要移动的目标移动距离,用于展开或收起滑动的按钮
  100. scrollX: 0, // movable-view移动过程中产生的change事件中的x轴移动值
  101. status: false, // 滑动的状态,表示当前是展开还是关闭按钮的状态
  102. movableAreaWidth: 0, // 滑动区域
  103. elId: this.$u.guid(), // id,用于通知另外组件关闭时的识别
  104. showBtn: false, // 刚开始渲染视图时不显示右边的按钮,避免视图闪动
  105. };
  106. },
  107. computed: {
  108. movableViewWidth() {
  109. return this.movableAreaWidth + this.allBtnWidth + 'px';
  110. },
  111. innerBtnWidth() {
  112. return uni.upx2px(this.btnWidth);
  113. },
  114. allBtnWidth() {
  115. return uni.upx2px(this.btnWidth) * this.options.length;
  116. },
  117. btnStyle() {
  118. return style => {
  119. let css = {};
  120. style.width = this.btnWidth + 'rpx';
  121. return style;
  122. };
  123. }
  124. },
  125. mounted() {
  126. this.getActionRect();
  127. // 等视图更新完后,再显示右边的可滑动按钮,防止这些按钮会"闪一下"
  128. setTimeout(() => {
  129. this.showBtn = true;
  130. }, 10);
  131. },
  132. methods: {
  133. // 点击按钮
  134. btnClick(index) {
  135. this.status = false;
  136. // this.index为点击的几个组件,index为点击某个组件的第几个按钮(options数组的索引)
  137. this.$emit('click', this.index, index);
  138. },
  139. // movable-view元素移动事件
  140. change(e) {
  141. this.scrollX = e.detail.x;
  142. },
  143. // 关闭按钮状态
  144. close() {
  145. this.moveX = 0;
  146. this.status = false;
  147. },
  148. // 打开按钮的状态
  149. open() {
  150. if (this.disabled) return;
  151. this.moveX = -this.allBtnWidth;
  152. this.status = true;
  153. },
  154. // 用户手指离开movable-view元素,停止触摸
  155. touchend() {
  156. this.moveX = this.scrollX;
  157. // 停止触摸时候,判断当前是展开还是关闭状态
  158. // 关闭状态
  159. // 这一步很重要,需要先给this.moveX一个变化的随机值,否则因为前后设置的为同一个值
  160. // props单向数据流的原因,导致movable-view元素不会发生变化,切记,详见文档:
  161. // https://uniapp.dcloud.io/use?id=%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98
  162. this.$nextTick(function() {
  163. if (this.status == false) {
  164. // 关闭状态左滑,产生的x轴位移为负值,也就是说滑动的距离大于按钮的四分之一宽度,自动展开按钮
  165. if (this.scrollX <= -this.allBtnWidth / 4) {
  166. this.moveX = -this.allBtnWidth; // 按钮宽度的负值,即为展开状态movable-view元素左滑的距离
  167. this.status = true; // 标志当前为展开状态
  168. this.emitOpenEvent();
  169. // 产生震动效果
  170. if (this.vibrateShort) uni.vibrateShort();
  171. } else {
  172. this.moveX = 0; // 如果距离没有按钮宽度的四分之一,自动收起
  173. this.status = false;
  174. this.emitCloseEvent();
  175. }
  176. } else {
  177. // 如果在打开的状态下,右滑动的距离X轴偏移超过按钮的四分之一(负值反过来的四分之三),自动收起按钮
  178. if (this.scrollX > (-this.allBtnWidth * 3) / 4) {
  179. this.moveX = 0;
  180. this.$nextTick(() => {
  181. this.moveX = 101;
  182. });
  183. this.status = false;
  184. this.emitCloseEvent();
  185. } else {
  186. this.moveX = -this.allBtnWidth;
  187. this.status = true;
  188. this.emitOpenEvent();
  189. }
  190. }
  191. });
  192. },
  193. emitOpenEvent() {
  194. this.$emit('open', this.index);
  195. },
  196. emitCloseEvent() {
  197. this.$emit('close', this.index);
  198. },
  199. // 开始触摸
  200. touchstart() {},
  201. getActionRect() {
  202. this.$uGetRect('.u-swipe-action').then(res => {
  203. this.movableAreaWidth = res.width;
  204. });
  205. },
  206. // 点击内容触发事件
  207. contentClick() {
  208. // 点击内容时,如果当前为打开状态,收起组件
  209. if (this.status == true) {
  210. this.status = 'close';
  211. this.moveX = 0;
  212. }
  213. this.$emit('content-click', this.index);
  214. }
  215. }
  216. };
  217. </script>
  218. <style scoped lang="scss">
  219. .u-swipe-action {
  220. width: auto;
  221. height: initial;
  222. position: relative;
  223. overflow: hidden;
  224. }
  225. .u-swipe-view {
  226. display: flex;
  227. height: initial;
  228. position: relative;
  229. /* 这一句很关键,覆盖默认的绝对定位 */
  230. }
  231. .u-swipe-content {
  232. flex: 1;
  233. }
  234. .u-swipe-del {
  235. position: relative;
  236. font-size: 30rpx;
  237. color: #ffffff;
  238. }
  239. .u-btn-text {
  240. position: absolute;
  241. top: 50%;
  242. left: 50%;
  243. transform: translate(-50%, -50%);
  244. }
  245. </style>