index.vue 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.stop.prevent="endDrag" @touchcancel.stop.prevent="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. const THRESHOLD = 0.3;
  19. VantComponent({
  20. props: {
  21. disabled: Boolean,
  22. leftWidth: {
  23. type: Number,
  24. value: 0
  25. },
  26. rightWidth: {
  27. type: Number,
  28. value: 0
  29. },
  30. asyncClose: Boolean
  31. },
  32. mixins: [touch],
  33. data: {
  34. catchMove: false
  35. },
  36. created() {
  37. this.offset = 0;
  38. },
  39. methods: {
  40. open(position) {
  41. const { leftWidth, rightWidth } = this.data;
  42. const offset = position === 'left' ? leftWidth : -rightWidth;
  43. this.swipeMove(offset);
  44. },
  45. close() {
  46. this.swipeMove(0);
  47. },
  48. swipeMove(offset = 0) {
  49. this.offset = offset;
  50. const transform = `translate3d(${offset}px, 0, 0)`;
  51. const transition = this.draging
  52. ? 'none'
  53. : '.6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  54. this.set({
  55. wrapperStyle: `
  56. -webkit-transform: ${transform};
  57. -webkit-transition: ${transition};
  58. transform: ${transform};
  59. transition: ${transition};
  60. `
  61. });
  62. },
  63. swipeLeaveTransition() {
  64. const { leftWidth, rightWidth } = this.data;
  65. const { offset } = this;
  66. if (rightWidth > 0 && -offset > rightWidth * THRESHOLD) {
  67. this.open('right');
  68. }
  69. else if (leftWidth > 0 && offset > leftWidth * THRESHOLD) {
  70. this.open('left');
  71. }
  72. else {
  73. this.swipeMove(0);
  74. }
  75. this.set({ catchMove: false });
  76. },
  77. startDrag(event) {
  78. if (this.data.disabled) {
  79. return;
  80. }
  81. this.draging = true;
  82. this.startOffset = this.offset;
  83. this.firstDirection = '';
  84. this.touchStart(event);
  85. },
  86. noop() { },
  87. onDrag(event) {
  88. if (this.data.disabled) {
  89. return;
  90. }
  91. this.touchMove(event);
  92. if (!this.firstDirection) {
  93. this.firstDirection = this.direction;
  94. this.set({ catchMove: this.firstDirection === 'horizontal' });
  95. }
  96. if (this.firstDirection === 'vertical') {
  97. return;
  98. }
  99. const { leftWidth, rightWidth } = this.data;
  100. const offset = this.startOffset + this.deltaX;
  101. if ((rightWidth > 0 && -offset > rightWidth) ||
  102. (leftWidth > 0 && offset > leftWidth)) {
  103. return;
  104. }
  105. this.swipeMove(offset);
  106. },
  107. endDrag() {
  108. if (this.data.disabled) {
  109. return;
  110. }
  111. this.draging = false;
  112. this.swipeLeaveTransition();
  113. },
  114. onClick(event) {
  115. const { key: position = 'outside' } = event.currentTarget.dataset;
  116. this.$emit('click', position);
  117. if (!this.offset) {
  118. return;
  119. }
  120. if (this.data.asyncClose) {
  121. this.$emit('close', { position, instance: this });
  122. }
  123. else {
  124. this.swipeMove(0);
  125. }
  126. }
  127. }
  128. });
  129. export default global['__wxComponents']['vant/swipe-cell/index']
  130. </script>
  131. <style platform="mp-weixin">
  132. @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)}
  133. </style>