index.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.newValue = this.startValue + diff;
  53. this.updateValue(this.newValue, false, true);
  54. });
  55. },
  56. onTouchEnd() {
  57. if (this.data.disabled)
  58. return;
  59. this.updateValue(this.newValue, true);
  60. },
  61. onClick(event) {
  62. if (this.data.disabled)
  63. return;
  64. this.getRect('.van-slider').then((rect) => {
  65. const value = (event.detail.x - rect.left) / rect.width * 100;
  66. this.updateValue(value, true);
  67. });
  68. },
  69. updateValue(value, end, drag) {
  70. value = this.format(value);
  71. this.set({
  72. value,
  73. barStyle: `width: ${value}%; height: ${this.data.barHeight};`
  74. });
  75. if (drag) {
  76. this.$emit('drag', { value });
  77. }
  78. if (end) {
  79. this.$emit('change', value);
  80. }
  81. },
  82. format(value) {
  83. const { max, min, step } = this.data;
  84. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  85. }
  86. }
  87. });