1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { VantComponent } from '../common/component';
  2. import { touch } from '../mixins/touch';
  3. VantComponent({
  4. mixins: [touch],
  5. props: {
  6. disabled: Boolean,
  7. useButtonSlot: Boolean,
  8. activeColor: String,
  9. inactiveColor: String,
  10. max: {
  11. type: Number,
  12. value: 100
  13. },
  14. min: {
  15. type: Number,
  16. value: 0
  17. },
  18. step: {
  19. type: Number,
  20. value: 1
  21. },
  22. value: {
  23. type: Number,
  24. value: 0
  25. },
  26. barHeight: {
  27. type: String,
  28. value: '2px'
  29. }
  30. },
  31. watch: {
  32. value(value) {
  33. this.updateValue(value, false);
  34. }
  35. },
  36. created() {
  37. this.updateValue(this.data.value);
  38. },
  39. methods: {
  40. onTouchStart(event) {
  41. if (this.data.disabled)
  42. return;
  43. this.touchStart(event);
  44. this.startValue = this.format(this.data.value);
  45. },
  46. onTouchMove(event) {
  47. if (this.data.disabled)
  48. return;
  49. this.touchMove(event);
  50. this.getRect('.van-slider').then(rect => {
  51. const diff = this.deltaX / rect.width * 100;
  52. this.updateValue(this.startValue + diff, false, true);
  53. });
  54. },
  55. onTouchEnd() {
  56. if (this.data.disabled)
  57. return;
  58. this.updateValue(this.data.value, true);
  59. },
  60. onClick(event) {
  61. if (this.data.disabled)
  62. return;
  63. this.getRect(rect => {
  64. const value = (event.detail.x - rect.left) / rect.width * 100;
  65. this.updateValue(value, true);
  66. });
  67. },
  68. updateValue(value, end, drag) {
  69. value = this.format(value);
  70. this.set({
  71. value,
  72. barStyle: `width: ${value}%; height: ${this.data.barHeight};`
  73. });
  74. if (drag) {
  75. this.$emit('drag', { value });
  76. }
  77. if (end) {
  78. this.$emit('change', value);
  79. }
  80. },
  81. format(value) {
  82. const { max, min, step } = this.data;
  83. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  84. }
  85. }
  86. });