index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 } from '../common/utils';
  26. VantComponent({
  27. mixins: [touch],
  28. props: {
  29. range: Boolean,
  30. disabled: Boolean,
  31. useButtonSlot: Boolean,
  32. activeColor: String,
  33. inactiveColor: String,
  34. max: {
  35. type: Number,
  36. value: 100,
  37. },
  38. min: {
  39. type: Number,
  40. value: 0,
  41. },
  42. step: {
  43. type: Number,
  44. value: 1,
  45. },
  46. value: {
  47. type: null,
  48. value: 0,
  49. observer(val) {
  50. if (val !== this.value) {
  51. this.updateValue(val);
  52. }
  53. },
  54. },
  55. vertical: Boolean,
  56. barHeight: null,
  57. },
  58. created() {
  59. this.updateValue(this.data.value);
  60. },
  61. methods: {
  62. onTouchStart(event) {
  63. if (this.data.disabled)
  64. return;
  65. const { index } = event.currentTarget.dataset;
  66. if (typeof index === 'number') {
  67. this.buttonIndex = index;
  68. }
  69. this.touchStart(event);
  70. this.startValue = this.format(this.value);
  71. this.newValue = this.value;
  72. if (this.isRange(this.newValue)) {
  73. this.startValue = this.newValue.map((val) => this.format(val));
  74. }
  75. else {
  76. this.startValue = this.format(this.newValue);
  77. }
  78. this.dragStatus = 'start';
  79. },
  80. onTouchMove(event) {
  81. if (this.data.disabled)
  82. return;
  83. if (this.dragStatus === 'start') {
  84. this.$emit('drag-start');
  85. }
  86. this.touchMove(event);
  87. this.dragStatus = 'draging';
  88. getRect(this, '.van-slider').then((rect) => {
  89. const { vertical } = this.data;
  90. const delta = vertical ? this.deltaY : this.deltaX;
  91. const total = vertical ? rect.height : rect.width;
  92. const diff = (delta / total) * this.getRange();
  93. if (this.isRange(this.startValue)) {
  94. this.newValue[this.buttonIndex] =
  95. this.startValue[this.buttonIndex] + diff;
  96. }
  97. else {
  98. this.newValue = this.startValue + diff;
  99. }
  100. this.updateValue(this.newValue, false, true);
  101. });
  102. },
  103. onTouchEnd() {
  104. if (this.data.disabled)
  105. return;
  106. if (this.dragStatus === 'draging') {
  107. this.updateValue(this.newValue, true);
  108. this.$emit('drag-end');
  109. }
  110. },
  111. onClick(event) {
  112. if (this.data.disabled)
  113. return;
  114. const { min } = this.data;
  115. getRect(this, '.van-slider').then((rect) => {
  116. const { vertical } = this.data;
  117. const touch = event.touches[0];
  118. const delta = vertical
  119. ? touch.clientY - rect.top
  120. : touch.clientX - rect.left;
  121. const total = vertical ? rect.height : rect.width;
  122. const value = Number(min) + (delta / total) * this.getRange();
  123. if (this.isRange(this.value)) {
  124. const [left, right] = this.value;
  125. const middle = (left + right) / 2;
  126. if (value <= middle) {
  127. this.updateValue([value, right], true);
  128. }
  129. else {
  130. this.updateValue([left, value], true);
  131. }
  132. }
  133. else {
  134. this.updateValue(value, true);
  135. }
  136. });
  137. },
  138. isRange(val) {
  139. const { range } = this.data;
  140. return range && Array.isArray(val);
  141. },
  142. handleOverlap(value) {
  143. if (value[0] > value[1]) {
  144. return value.slice(0).reverse();
  145. }
  146. return value;
  147. },
  148. updateValue(value, end, drag) {
  149. if (this.isRange(value)) {
  150. value = this.handleOverlap(value).map((val) => this.format(val));
  151. }
  152. else {
  153. value = this.format(value);
  154. }
  155. this.value = value;
  156. const { vertical } = this.data;
  157. const mainAxis = vertical ? 'height' : 'width';
  158. this.setData({
  159. wrapperStyle: `
  160. background: ${this.data.inactiveColor || ''};
  161. ${vertical ? 'width' : 'height'}: ${addUnit(this.data.barHeight) || ''};
  162. `,
  163. barStyle: `
  164. ${mainAxis}: ${this.calcMainAxis()};
  165. left: ${vertical ? 0 : this.calcOffset()};
  166. top: ${vertical ? this.calcOffset() : 0};
  167. ${drag ? 'transition: none;' : ''}
  168. `,
  169. });
  170. if (drag) {
  171. this.$emit('drag', { value });
  172. }
  173. if (end) {
  174. this.$emit('change', value);
  175. }
  176. if ((drag || end) && canIUseModel()) {
  177. this.setData({ value });
  178. }
  179. },
  180. getScope() {
  181. return Number(this.data.max) - Number(this.data.min);
  182. },
  183. getRange() {
  184. const { max, min } = this.data;
  185. return max - min;
  186. },
  187. // 计算选中条的长度百分比
  188. calcMainAxis() {
  189. const { value } = this;
  190. const { min } = this.data;
  191. const scope = this.getScope();
  192. if (this.isRange(value)) {
  193. return `${((value[1] - value[0]) * 100) / scope}%`;
  194. }
  195. return `${((value - Number(min)) * 100) / scope}%`;
  196. },
  197. // 计算选中条的开始位置的偏移量
  198. calcOffset() {
  199. const { value } = this;
  200. const { min } = this.data;
  201. const scope = this.getScope();
  202. if (this.isRange(value)) {
  203. return `${((value[0] - Number(min)) * 100) / scope}%`;
  204. }
  205. return '0%';
  206. },
  207. format(value) {
  208. const { max, min, step } = this.data;
  209. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  210. },
  211. },
  212. });
  213. export default global['__wxComponents']['vant/slider/index']
  214. </script>
  215. <style platform="mp-weixin">
  216. @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}
  217. </style>