index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <uni-shadow-root class="vant-swipe-cell-index"><view class="van-swipe-cell" data-key="cell" @click.stop.prevent="onClick" @touchstart="startDrag" @touchmove.stop.prevent="_$self[(catchMove ? 'noop' : '')||'_$noop']($event)" @touchmove.capture="onDrag" @touchend="endDrag" @touchcancel="endDrag">
  3. <view :style="wrapperStyle">
  4. <view v-if="leftWidth" class="van-swipe-cell__left" data-key="left" @click.stop.prevent="onClick">
  5. <slot name="left"></slot>
  6. </view>
  7. <slot></slot>
  8. <view v-if="rightWidth" class="van-swipe-cell__right" data-key="right" @click.stop.prevent="onClick">
  9. <slot name="right"></slot>
  10. </view>
  11. </view>
  12. </view></uni-shadow-root>
  13. </template>
  14. <script>
  15. global['__wxRoute'] = 'vant/swipe-cell/index'
  16. import { VantComponent } from '../common/component';
  17. import { touch } from '../mixins/touch';
  18. import { range } from '../common/utils';
  19. const THRESHOLD = 0.3;
  20. let ARRAY = [];
  21. VantComponent({
  22. props: {
  23. disabled: Boolean,
  24. leftWidth: {
  25. type: Number,
  26. value: 0
  27. },
  28. rightWidth: {
  29. type: Number,
  30. value: 0
  31. },
  32. asyncClose: Boolean,
  33. name: {
  34. type: [Number, String],
  35. value: ''
  36. }
  37. },
  38. mixins: [touch],
  39. data: {
  40. catchMove: false
  41. },
  42. created() {
  43. this.offset = 0;
  44. ARRAY.push(this);
  45. },
  46. destroyed() {
  47. ARRAY = ARRAY.filter(item => item !== this);
  48. },
  49. methods: {
  50. open(position) {
  51. const { leftWidth, rightWidth } = this.data;
  52. const offset = position === 'left' ? leftWidth : -rightWidth;
  53. this.swipeMove(offset);
  54. this.$emit('open', {
  55. position,
  56. name: this.data.name
  57. });
  58. },
  59. close() {
  60. this.swipeMove(0);
  61. },
  62. swipeMove(offset = 0) {
  63. this.offset = range(offset, -this.data.rightWidth, this.data.leftWidth);
  64. const transform = `translate3d(${this.offset}px, 0, 0)`;
  65. const transition = this.dragging
  66. ? 'none'
  67. : 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  68. this.setData({
  69. wrapperStyle: `
  70. -webkit-transform: ${transform};
  71. -webkit-transition: ${transition};
  72. transform: ${transform};
  73. transition: ${transition};
  74. `
  75. });
  76. },
  77. swipeLeaveTransition() {
  78. const { leftWidth, rightWidth } = this.data;
  79. const { offset } = this;
  80. if (rightWidth > 0 && -offset > rightWidth * THRESHOLD) {
  81. this.open('right');
  82. }
  83. else if (leftWidth > 0 && offset > leftWidth * THRESHOLD) {
  84. this.open('left');
  85. }
  86. else {
  87. this.swipeMove(0);
  88. }
  89. this.setData({ catchMove: false });
  90. },
  91. startDrag(event) {
  92. if (this.data.disabled) {
  93. return;
  94. }
  95. this.startOffset = this.offset;
  96. this.touchStart(event);
  97. },
  98. noop() { },
  99. onDrag(event) {
  100. if (this.data.disabled) {
  101. return;
  102. }
  103. this.touchMove(event);
  104. if (this.direction !== 'horizontal') {
  105. return;
  106. }
  107. this.dragging = true;
  108. ARRAY.filter(item => item !== this).forEach(item => item.close());
  109. this.setData({ catchMove: true });
  110. this.swipeMove(this.startOffset + this.deltaX);
  111. },
  112. endDrag() {
  113. if (this.data.disabled) {
  114. return;
  115. }
  116. this.dragging = false;
  117. this.swipeLeaveTransition();
  118. },
  119. onClick(event) {
  120. const { key: position = 'outside' } = event.currentTarget.dataset;
  121. this.$emit('click', position);
  122. if (!this.offset) {
  123. return;
  124. }
  125. if (this.data.asyncClose) {
  126. this.$emit('close', {
  127. position,
  128. instance: this,
  129. name: this.data.name
  130. });
  131. }
  132. else {
  133. this.swipeMove(0);
  134. }
  135. }
  136. }
  137. });
  138. export default global['__wxComponents']['vant/swipe-cell/index']
  139. </script>
  140. <style platform="mp-weixin">
  141. @import '../common/index.css';.van-swipe-cell{position:relative;overflow:hidden}.van-swipe-cell__left,.van-swipe-cell__right{position:absolute;top:0;height:100%}.van-swipe-cell__left{left:0;-webkit-transform:translate3d(-100%,0,0);transform:translate3d(-100%,0,0)}.van-swipe-cell__right{right:0;-webkit-transform:translate3d(100%,0,0);transform:translate3d(100%,0,0)}
  142. </style>