index.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { VantComponent } from '../common/component';
  2. import { touch } from '../mixins/touch';
  3. const THRESHOLD = 0.15;
  4. VantComponent({
  5. props: {
  6. disabled: Boolean,
  7. leftWidth: {
  8. type: Number,
  9. value: 0
  10. },
  11. rightWidth: {
  12. type: Number,
  13. value: 0
  14. },
  15. asyncClose: Boolean
  16. },
  17. mixins: [touch],
  18. data: {
  19. offset: 0,
  20. draging: false
  21. },
  22. computed: {
  23. wrapperStyle() {
  24. const { offset, draging } = this.data;
  25. const transform = `translate3d(${offset}px, 0, 0)`;
  26. const transition = draging ? 'none' : '.6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  27. return `
  28. -webkit-transform: ${transform};
  29. -webkit-transition: ${transition};
  30. transform: ${transform};
  31. transition: ${transition};
  32. `;
  33. }
  34. },
  35. methods: {
  36. onTransitionend() {
  37. this.swipe = false;
  38. },
  39. open(position) {
  40. const { leftWidth, rightWidth } = this.data;
  41. const offset = position === 'left' ? leftWidth : -rightWidth;
  42. this.swipeMove(offset);
  43. this.resetSwipeStatus();
  44. },
  45. close() {
  46. this.set({ offset: 0 });
  47. },
  48. resetSwipeStatus() {
  49. this.swiping = false;
  50. this.opened = true;
  51. },
  52. swipeMove(offset = 0) {
  53. this.set({ offset });
  54. offset && (this.swiping = true);
  55. !offset && (this.opened = false);
  56. },
  57. swipeLeaveTransition(direction) {
  58. const { offset, leftWidth, rightWidth } = this.data;
  59. const threshold = this.opened ? 1 - THRESHOLD : THRESHOLD;
  60. // right
  61. if (direction > 0 && -offset > rightWidth * threshold && rightWidth > 0) {
  62. this.open('right');
  63. // left
  64. }
  65. else if (direction < 0 && offset > leftWidth * threshold && leftWidth > 0) {
  66. this.open('left');
  67. }
  68. else {
  69. this.swipeMove();
  70. }
  71. },
  72. startDrag(event) {
  73. if (this.data.disabled) {
  74. return;
  75. }
  76. this.set({ draging: true });
  77. this.touchStart(event);
  78. if (this.opened) {
  79. this.startX -= this.data.offset;
  80. }
  81. },
  82. onDrag(event) {
  83. if (this.data.disabled) {
  84. return;
  85. }
  86. this.touchMove(event);
  87. const { deltaX } = this;
  88. const { leftWidth, rightWidth } = this.data;
  89. if ((deltaX < 0 && (-deltaX > rightWidth || !rightWidth)) ||
  90. (deltaX > 0 && (deltaX > leftWidth || (deltaX > 0 && !leftWidth)))) {
  91. return;
  92. }
  93. if (this.direction === 'horizontal') {
  94. this.swipeMove(deltaX);
  95. }
  96. },
  97. endDrag() {
  98. if (this.data.disabled) {
  99. return;
  100. }
  101. this.set({ draging: false });
  102. if (this.swiping) {
  103. this.swipeLeaveTransition(this.data.offset > 0 ? -1 : 1);
  104. }
  105. },
  106. onClick(event) {
  107. const { key: position = 'outside' } = event.currentTarget.dataset;
  108. this.$emit('click', position);
  109. if (!this.data.offset) {
  110. return;
  111. }
  112. if (this.data.asyncClose) {
  113. this.$emit('close', { position, instance: this });
  114. }
  115. else {
  116. this.swipeMove(0);
  117. }
  118. }
  119. }
  120. });