index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <uni-shadow-root class="vant-slider-index"><view :class="'custom-class '+(utils.bem('slider', { disabled, vertical }))" :style="wrapperStyle" @click="onClick">
  3. <view :class="utils.bem('slider__bar')" :style="(barStyle)+'; '+(style({ backgroundColor: activeColor }))">
  4. <view v-if="range" :class="utils.bem('slider__button-wrapper-left')" :data-index="0" @touchstart="onTouchStart" @touchmove.stop.prevent="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
  5. <slot v-if="useButtonSlot" name="left-button"></slot>
  6. <view v-else :class="utils.bem('slider__button')"></view>
  7. </view>
  8. <view v-if="range" :class="utils.bem('slider__button-wrapper-right')" :data-index="1" @touchstart="onTouchStart" @touchmove.stop.prevent="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
  9. <slot v-if="useButtonSlot" name="right-button"></slot>
  10. <view v-else :class="utils.bem('slider__button')"></view>
  11. </view>
  12. <view v-if="(!range)" :class="utils.bem('slider__button-wrapper')" @touchstart="onTouchStart" @touchmove.stop.prevent="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
  13. <slot v-if="useButtonSlot" name="button"></slot>
  14. <view v-else :class="utils.bem('slider__button')"></view>
  15. </view>
  16. </view>
  17. </view></uni-shadow-root>
  18. </template>
  19. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="../wxs/style.wxs" module="style"></wxs>
  20. <script>
  21. global['__wxRoute'] = 'vant/slider/index'
  22. import { VantComponent } from '../common/component';
  23. import { touch } from '../mixins/touch';
  24. import { canIUseModel } from '../common/version';
  25. import { getRect, addUnit, nextTick, addNumber, clamp } from '../common/utils';
  26. const DRAG_STATUS = {
  27. START: 'start',
  28. MOVING: 'moving',
  29. END: 'end',
  30. };
  31. VantComponent({
  32. mixins: [touch],
  33. props: {
  34. range: Boolean,
  35. disabled: Boolean,
  36. useButtonSlot: Boolean,
  37. activeColor: String,
  38. inactiveColor: String,
  39. max: {
  40. type: Number,
  41. value: 100,
  42. },
  43. min: {
  44. type: Number,
  45. value: 0,
  46. },
  47. step: {
  48. type: Number,
  49. value: 1,
  50. },
  51. value: {
  52. type: null,
  53. value: 0,
  54. observer(val) {
  55. if (val !== this.value) {
  56. this.updateValue(val);
  57. }
  58. },
  59. },
  60. vertical: Boolean,
  61. barHeight: null,
  62. },
  63. created() {
  64. this.updateValue(this.data.value);
  65. },
  66. methods: {
  67. onTouchStart(event) {
  68. if (this.data.disabled)
  69. return;
  70. const { index } = event.currentTarget.dataset;
  71. if (typeof index === 'number') {
  72. this.buttonIndex = index;
  73. }
  74. this.touchStart(event);
  75. this.startValue = this.format(this.value);
  76. this.newValue = this.value;
  77. if (this.isRange(this.newValue)) {
  78. this.startValue = this.newValue.map((val) => this.format(val));
  79. }
  80. else {
  81. this.startValue = this.format(this.newValue);
  82. }
  83. this.dragStatus = DRAG_STATUS.START;
  84. },
  85. onTouchMove(event) {
  86. if (this.data.disabled)
  87. return;
  88. if (this.dragStatus === DRAG_STATUS.START) {
  89. this.$emit('drag-start');
  90. }
  91. this.touchMove(event);
  92. this.dragStatus = DRAG_STATUS.MOVING;
  93. getRect(this, '.van-slider').then((rect) => {
  94. const { vertical } = this.data;
  95. const delta = vertical ? this.deltaY : this.deltaX;
  96. const total = vertical ? rect.height : rect.width;
  97. const diff = (delta / total) * this.getRange();
  98. if (this.isRange(this.startValue)) {
  99. this.newValue[this.buttonIndex] =
  100. this.startValue[this.buttonIndex] + diff;
  101. }
  102. else {
  103. this.newValue = this.startValue + diff;
  104. }
  105. this.updateValue(this.newValue, false, true);
  106. });
  107. },
  108. onTouchEnd() {
  109. if (this.data.disabled)
  110. return;
  111. if (this.dragStatus === DRAG_STATUS.MOVING) {
  112. this.dragStatus = DRAG_STATUS.END;
  113. nextTick(() => {
  114. this.updateValue(this.newValue, true);
  115. this.$emit('drag-end');
  116. });
  117. }
  118. },
  119. onClick(event) {
  120. if (this.data.disabled)
  121. return;
  122. const { min } = this.data;
  123. getRect(this, '.van-slider').then((rect) => {
  124. const { vertical } = this.data;
  125. const touch = event.touches[0];
  126. const delta = vertical
  127. ? touch.clientY - rect.top
  128. : touch.clientX - rect.left;
  129. const total = vertical ? rect.height : rect.width;
  130. const value = Number(min) + (delta / total) * this.getRange();
  131. if (this.isRange(this.value)) {
  132. const [left, right] = this.value;
  133. const middle = (left + right) / 2;
  134. if (value <= middle) {
  135. this.updateValue([value, right], true);
  136. }
  137. else {
  138. this.updateValue([left, value], true);
  139. }
  140. }
  141. else {
  142. this.updateValue(value, true);
  143. }
  144. });
  145. },
  146. isRange(val) {
  147. const { range } = this.data;
  148. return range && Array.isArray(val);
  149. },
  150. handleOverlap(value) {
  151. if (value[0] > value[1]) {
  152. return value.slice(0).reverse();
  153. }
  154. return value;
  155. },
  156. updateValue(value, end, drag) {
  157. if (this.isRange(value)) {
  158. value = this.handleOverlap(value).map((val) => this.format(val));
  159. }
  160. else {
  161. value = this.format(value);
  162. }
  163. this.value = value;
  164. const { vertical } = this.data;
  165. const mainAxis = vertical ? 'height' : 'width';
  166. this.setData({
  167. wrapperStyle: `
  168. background: ${this.data.inactiveColor || ''};
  169. ${vertical ? 'width' : 'height'}: ${addUnit(this.data.barHeight) || ''};
  170. `,
  171. barStyle: `
  172. ${mainAxis}: ${this.calcMainAxis()};
  173. left: ${vertical ? 0 : this.calcOffset()};
  174. top: ${vertical ? this.calcOffset() : 0};
  175. ${drag ? 'transition: none;' : ''}
  176. `,
  177. });
  178. if (drag) {
  179. this.$emit('drag', { value });
  180. }
  181. if (end) {
  182. this.$emit('change', value);
  183. }
  184. if ((drag || end) && canIUseModel()) {
  185. this.setData({ value });
  186. }
  187. },
  188. getScope() {
  189. return Number(this.data.max) - Number(this.data.min);
  190. },
  191. getRange() {
  192. const { max, min } = this.data;
  193. return max - min;
  194. },
  195. getOffsetWidth(current, min) {
  196. const scope = this.getScope();
  197. // 避免最小值小于最小step时出现负数情况
  198. return `${Math.max(((current - min) * 100) / scope, 0)}%`;
  199. },
  200. // 计算选中条的长度百分比
  201. calcMainAxis() {
  202. const { value } = this;
  203. const { min } = this.data;
  204. if (this.isRange(value)) {
  205. return this.getOffsetWidth(value[1], value[0]);
  206. }
  207. return this.getOffsetWidth(value, Number(min));
  208. },
  209. // 计算选中条的开始位置的偏移量
  210. calcOffset() {
  211. const { value } = this;
  212. const { min } = this.data;
  213. const scope = this.getScope();
  214. if (this.isRange(value)) {
  215. return `${((value[0] - Number(min)) * 100) / scope}%`;
  216. }
  217. return '0%';
  218. },
  219. format(value) {
  220. const min = +this.data.min;
  221. const max = +this.data.max;
  222. const step = +this.data.step;
  223. value = clamp(value, min, max);
  224. const diff = Math.round((value - min) / step) * step;
  225. return addNumber(min, diff);
  226. },
  227. },
  228. });
  229. export default global['__wxComponents']['vant/slider/index']
  230. </script>
  231. <style platform="mp-weixin">
  232. @import '../common/index.css';.van-slider{background-color:var(--slider-inactive-background-color,#ebedf0);border-radius:999px;height:var(--slider-bar-height,2px);position:relative}.van-slider:before{bottom:calc(var(--padding-xs, 8px)*-1);content:"";left:0;position:absolute;right:0;top:calc(var(--padding-xs, 8px)*-1)}.van-slider__bar{background-color:var(--slider-active-background-color,#1989fa);border-radius:inherit;height:100%;position:relative;transition:all .2s;width:100%}.van-slider__button{background-color:var(--slider-button-background-color,#fff);border-radius:var(--slider-button-border-radius,50%);box-shadow:var(--slider-button-box-shadow,0 1px 2px rgba(0,0,0,.5));height:var(--slider-button-height,24px);width:var(--slider-button-width,24px)}.van-slider__button-wrapper,.van-slider__button-wrapper-right{position:absolute;right:0;top:50%;transform:translate3d(50%,-50%,0)}.van-slider__button-wrapper-left{left:0;position:absolute;top:50%;transform:translate3d(-50%,-50%,0)}.van-slider--disabled{opacity:var(--slider-disabled-opacity,.5)}.van-slider--vertical{display:inline-block;height:100%;width:var(--slider-bar-height,2px)}.van-slider--vertical .van-slider__button-wrapper,.van-slider--vertical .van-slider__button-wrapper-right{bottom:0;right:50%;top:auto;transform:translate3d(50%,50%,0)}.van-slider--vertical .van-slider__button-wrapper-left{left:auto;right:50%;top:0;transform:translate3d(50%,-50%,0)}.van-slider--vertical:before{bottom:0;left:-8px;right:-8px;top:0}
  233. </style>