index.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { VantComponent } from '../common/component';
  2. import { touch } from '../mixins/touch';
  3. const THRESHOLD = 0.3;
  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. catchMove: false
  20. },
  21. created() {
  22. this.offset = 0;
  23. },
  24. methods: {
  25. open(position) {
  26. const { leftWidth, rightWidth } = this.data;
  27. const offset = position === 'left' ? leftWidth : -rightWidth;
  28. this.swipeMove(offset);
  29. },
  30. close() {
  31. this.swipeMove(0);
  32. },
  33. swipeMove(offset = 0) {
  34. this.offset = offset;
  35. const transform = `translate3d(${offset}px, 0, 0)`;
  36. const transition = this.draging
  37. ? 'none'
  38. : '.6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  39. this.set({
  40. wrapperStyle: `
  41. -webkit-transform: ${transform};
  42. -webkit-transition: ${transition};
  43. transform: ${transform};
  44. transition: ${transition};
  45. `
  46. });
  47. },
  48. swipeLeaveTransition() {
  49. const { leftWidth, rightWidth } = this.data;
  50. const { offset } = this;
  51. if (rightWidth > 0 && -offset > rightWidth * THRESHOLD) {
  52. this.open('right');
  53. }
  54. else if (leftWidth > 0 && offset > leftWidth * THRESHOLD) {
  55. this.open('left');
  56. }
  57. else {
  58. this.swipeMove(0);
  59. }
  60. this.set({ catchMove: false });
  61. },
  62. startDrag(event) {
  63. if (this.data.disabled) {
  64. return;
  65. }
  66. this.draging = true;
  67. this.startOffset = this.offset;
  68. this.firstDirection = '';
  69. this.touchStart(event);
  70. },
  71. noop() { },
  72. onDrag(event) {
  73. if (this.data.disabled) {
  74. return;
  75. }
  76. this.touchMove(event);
  77. if (!this.firstDirection) {
  78. this.firstDirection = this.direction;
  79. this.set({ catchMove: this.firstDirection === 'horizontal' });
  80. }
  81. if (this.firstDirection === 'vertical') {
  82. return;
  83. }
  84. const { leftWidth, rightWidth } = this.data;
  85. const offset = this.startOffset + this.deltaX;
  86. if ((rightWidth > 0 && -offset > rightWidth) ||
  87. (leftWidth > 0 && offset > leftWidth)) {
  88. return;
  89. }
  90. this.swipeMove(offset);
  91. },
  92. endDrag() {
  93. if (this.data.disabled) {
  94. return;
  95. }
  96. this.draging = false;
  97. this.swipeLeaveTransition();
  98. },
  99. onClick(event) {
  100. const { key: position = 'outside' } = event.currentTarget.dataset;
  101. this.$emit('click', position);
  102. if (!this.offset) {
  103. return;
  104. }
  105. if (this.data.asyncClose) {
  106. this.$emit('close', { position, instance: this });
  107. }
  108. else {
  109. this.swipeMove(0);
  110. }
  111. }
  112. }
  113. });