index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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)+';'+(computed.barStyle(barHeight, 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><wxs src="./index.wxs" module="computed"></wxs>
  12. <script>
  13. global['__wxRoute'] = 'vant/slider/index'
  14. import { VantComponent } from '../common/component';
  15. import { touch } from '../mixins/touch';
  16. import { canIUseModel } from '../common/version';
  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. observer(val) {
  40. if (val !== this.value) {
  41. this.updateValue(val);
  42. }
  43. },
  44. },
  45. barHeight: {
  46. type: null,
  47. value: 2,
  48. },
  49. },
  50. created() {
  51. this.updateValue(this.data.value);
  52. },
  53. methods: {
  54. onTouchStart(event) {
  55. if (this.data.disabled) return;
  56. this.touchStart(event);
  57. this.startValue = this.format(this.value);
  58. this.dragStatus = 'start';
  59. },
  60. onTouchMove(event) {
  61. if (this.data.disabled) return;
  62. if (this.dragStatus === 'start') {
  63. this.$emit('drag-start');
  64. }
  65. this.touchMove(event);
  66. this.dragStatus = 'draging';
  67. this.getRect('.van-slider').then((rect) => {
  68. const diff = (this.deltaX / rect.width) * 100;
  69. this.newValue = this.startValue + diff;
  70. this.updateValue(this.newValue, false, true);
  71. });
  72. },
  73. onTouchEnd() {
  74. if (this.data.disabled) return;
  75. if (this.dragStatus === 'draging') {
  76. this.updateValue(this.newValue, true);
  77. this.$emit('drag-end');
  78. }
  79. },
  80. onClick(event) {
  81. if (this.data.disabled) return;
  82. const { min } = this.data;
  83. this.getRect('.van-slider').then((rect) => {
  84. const value =
  85. ((event.detail.x - rect.left) / rect.width) * this.getRange() + min;
  86. this.updateValue(value, true);
  87. });
  88. },
  89. updateValue(value, end, drag) {
  90. value = this.format(value);
  91. const { min } = this.data;
  92. const width = `${((value - min) * 100) / this.getRange()}%`;
  93. this.value = value;
  94. this.setData({
  95. barStyle: `
  96. width: ${width};
  97. ${drag ? 'transition: none;' : ''}
  98. `,
  99. });
  100. if (drag) {
  101. this.$emit('drag', { value });
  102. }
  103. if (end) {
  104. this.$emit('change', value);
  105. }
  106. if ((drag || end) && canIUseModel()) {
  107. this.setData({ value });
  108. }
  109. },
  110. getRange() {
  111. const { max, min } = this.data;
  112. return max - min;
  113. },
  114. format(value) {
  115. const { max, min, step } = this.data;
  116. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  117. },
  118. },
  119. });
  120. export default global['__wxComponents']['vant/slider/index']
  121. </script>
  122. <style platform="mp-weixin">
  123. @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}
  124. </style>