index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: function value(_value) {
  33. this.updateValue(_value, false);
  34. }
  35. },
  36. created: function created() {
  37. this.updateValue(this.data.value);
  38. },
  39. methods: {
  40. onTouchStart: function onTouchStart(event) {
  41. if (this.data.disabled) return;
  42. this.touchStart(event);
  43. this.startValue = this.format(this.data.value);
  44. },
  45. onTouchMove: function onTouchMove(event) {
  46. var _this = this;
  47. if (this.data.disabled) return;
  48. this.touchMove(event);
  49. this.getRect('.van-slider').then(function (rect) {
  50. var diff = _this.deltaX / rect.width * 100;
  51. _this.updateValue(_this.startValue + diff);
  52. });
  53. },
  54. onTouchEnd: function onTouchEnd() {
  55. if (this.data.disabled) return;
  56. this.updateValue(this.data.value, true);
  57. },
  58. onClick: function onClick(event) {
  59. var _this2 = this;
  60. if (this.data.disabled) return;
  61. this.getRect(function (rect) {
  62. var value = (event.detail.x - rect.left) / rect.width * 100;
  63. _this2.updateValue(value, true);
  64. });
  65. },
  66. updateValue: function updateValue(value, end) {
  67. value = this.format(value);
  68. this.set({
  69. value: value,
  70. barStyle: "width: " + value + "%; height: " + this.data.barHeight + ";"
  71. });
  72. this.$emit('drag', {
  73. value: value
  74. });
  75. if (end) {
  76. this.$emit('change', value);
  77. }
  78. },
  79. format: function format(value) {
  80. var _this$data = this.data,
  81. max = _this$data.max,
  82. min = _this$data.min,
  83. step = _this$data.step;
  84. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  85. }
  86. }
  87. });