index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. import { addUnit } from '../common/utils';
  17. VantComponent({
  18. mixins: [touch],
  19. props: {
  20. disabled: Boolean,
  21. useButtonSlot: Boolean,
  22. activeColor: String,
  23. inactiveColor: String,
  24. max: {
  25. type: Number,
  26. value: 100
  27. },
  28. min: {
  29. type: Number,
  30. value: 0
  31. },
  32. step: {
  33. type: Number,
  34. value: 1
  35. },
  36. value: {
  37. type: Number,
  38. value: 0
  39. },
  40. barHeight: {
  41. type: null,
  42. value: '2px'
  43. }
  44. },
  45. watch: {
  46. value(value) {
  47. this.updateValue(value, false);
  48. }
  49. },
  50. created() {
  51. this.updateValue(this.data.value);
  52. },
  53. methods: {
  54. onTouchStart(event) {
  55. if (this.data.disabled)
  56. return;
  57. this.touchStart(event);
  58. this.startValue = this.format(this.data.value);
  59. this.dragStatus = 'start';
  60. },
  61. onTouchMove(event) {
  62. if (this.data.disabled)
  63. return;
  64. if (this.dragStatus === 'start') {
  65. this.$emit('drag-start');
  66. }
  67. this.touchMove(event);
  68. this.dragStatus = 'draging';
  69. this.getRect('.van-slider').then((rect) => {
  70. const diff = this.deltaX / rect.width * 100;
  71. this.newValue = this.startValue + diff;
  72. this.updateValue(this.newValue, false, true);
  73. });
  74. },
  75. onTouchEnd() {
  76. if (this.data.disabled)
  77. return;
  78. if (this.dragStatus === 'draging') {
  79. this.updateValue(this.newValue, true);
  80. this.$emit('drag-end');
  81. }
  82. },
  83. onClick(event) {
  84. if (this.data.disabled)
  85. return;
  86. const { min } = this.data;
  87. this.getRect('.van-slider').then((rect) => {
  88. const value = (event.detail.x - rect.left) / rect.width * this.getRange() + min;
  89. this.updateValue(value, true);
  90. });
  91. },
  92. updateValue(value, end, drag) {
  93. value = this.format(value);
  94. const { barHeight, min } = this.data;
  95. const width = `${((value - min) * 100) / this.getRange()}%`;
  96. this.setData({
  97. value,
  98. barStyle: `
  99. width: ${width};
  100. height: ${addUnit(barHeight)};
  101. ${drag ? 'transition: none;' : ''}
  102. `,
  103. });
  104. if (drag) {
  105. this.$emit('drag', { value });
  106. }
  107. if (end) {
  108. this.$emit('change', value);
  109. }
  110. },
  111. getRange() {
  112. const { max, min } = this.data;
  113. return max - min;
  114. },
  115. format(value) {
  116. const { max, min, step } = this.data;
  117. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  118. }
  119. }
  120. });
  121. export default global['__wxComponents']['vant/slider/index']
  122. </script>
  123. <style platform="mp-weixin">
  124. @import '../common/index.css';.van-slider{position:relative;border-radius:999px;border-radius:var(--border-radius-max,999px);background-color:#ebedf0;background-color:var(--slider-inactive-background-color,#ebedf0)}.van-slider:before{position:absolute;right:0;left:0;content:"";top:-8px;top:-var(--padding-xs,8px);bottom:-8px;bottom:-var(--padding-xs,8px)}.van-slider__bar{position:relative;border-radius:inherit;transition:width .2s;transition:width var(--animation-duration-fast,.2s);background-color:#1989fa;background-color:var(--slider-active-background-color,#1989fa)}.van-slider__button{width:24px;height:24px;border-radius:50%;box-shadow:0 1px 2px rgba(0,0,0,.5);background-color:#fff;background-color:var(--slider-button-background-color,#fff)}.van-slider__button-wrapper{position:absolute;top:50%;right:0;-webkit-transform:translate3d(50%,-50%,0);transform:translate3d(50%,-50%,0)}.van-slider--disabled{opacity:.5}
  125. </style>