index.vue 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <uni-shadow-root class="vant-slider-index"><view :class="'custom-class '+(utils.bem('slider', { disabled }))" :style="inactiveColor ? 'background:' + inactiveColor : ''" @click="onClick">
  3. <view class="van-slider__bar" :style="(barStyle)+'; '+(activeColor ? 'background:' + activeColor : '')">
  4. <view class="van-slider__button-wrapper" @touchstart="onTouchStart" @touchmove.stop.prevent="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
  5. <slot v-if="useButtonSlot" name="button"></slot>
  6. <view v-else class="van-slider__button"></view>
  7. </view>
  8. </view>
  9. </view></uni-shadow-root>
  10. </template>
  11. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  12. <script>
  13. global['__wxRoute'] = 'vant/slider/index'
  14. import { VantComponent } from '../common/component';
  15. import { touch } from '../mixins/touch';
  16. VantComponent({
  17. mixins: [touch],
  18. props: {
  19. disabled: Boolean,
  20. useButtonSlot: Boolean,
  21. activeColor: String,
  22. inactiveColor: String,
  23. max: {
  24. type: Number,
  25. value: 100
  26. },
  27. min: {
  28. type: Number,
  29. value: 0
  30. },
  31. step: {
  32. type: Number,
  33. value: 1
  34. },
  35. value: {
  36. type: Number,
  37. value: 0
  38. },
  39. barHeight: {
  40. type: String,
  41. value: '2px'
  42. }
  43. },
  44. watch: {
  45. value(value) {
  46. this.updateValue(value, false);
  47. }
  48. },
  49. created() {
  50. this.updateValue(this.data.value);
  51. },
  52. methods: {
  53. onTouchStart(event) {
  54. if (this.data.disabled)
  55. return;
  56. this.touchStart(event);
  57. this.startValue = this.format(this.data.value);
  58. },
  59. onTouchMove(event) {
  60. if (this.data.disabled)
  61. return;
  62. this.touchMove(event);
  63. this.getRect('.van-slider').then((rect) => {
  64. const diff = this.deltaX / rect.width * 100;
  65. this.newValue = this.startValue + diff;
  66. this.updateValue(this.newValue, false, true);
  67. });
  68. },
  69. onTouchEnd() {
  70. if (this.data.disabled)
  71. return;
  72. this.updateValue(this.newValue, true);
  73. },
  74. onClick(event) {
  75. if (this.data.disabled)
  76. return;
  77. this.getRect('.van-slider').then((rect) => {
  78. const value = (event.detail.x - rect.left) / rect.width * 100;
  79. this.updateValue(value, true);
  80. });
  81. },
  82. updateValue(value, end, drag) {
  83. value = this.format(value);
  84. this.set({
  85. value,
  86. barStyle: `width: ${value}%; height: ${this.data.barHeight};`
  87. });
  88. if (drag) {
  89. this.$emit('drag', { value });
  90. }
  91. if (end) {
  92. this.$emit('change', value);
  93. }
  94. },
  95. format(value) {
  96. const { max, min, step } = this.data;
  97. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  98. }
  99. }
  100. });
  101. export default global['__wxComponents']['vant/slider/index']
  102. </script>
  103. <style platform="mp-weixin">
  104. @import '../common/index.css';.van-slider{position:relative;border-radius:999px;background-color:#e5e5e5}.van-slider__bar{position:relative;border-radius:inherit;background-color:#1989fa}.van-slider__button{width:20px;height:20px;border-radius:50%;background-color:#fff;box-shadow:0 1px 2px rgba(0,0,0,.5)}.van-slider__button-wrapper{position:absolute;top:50%;right:0;-webkit-transform:translate3d(50%,-50%,0);transform:translate3d(50%,-50%,0)}.van-slider__button-wrapper:after{content:"";position:absolute;width:200%;height:200%;top:-50%;left:-50%}.van-slider--disabled{opacity:.3}
  105. </style>