u-slider.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <template>
  2. <view
  3. class="u-slider"
  4. :style="[addStyle(customStyle)]"
  5. >
  6. <template v-if="!useNative || isRange">
  7. <view ref="u-slider-inner" class="u-slider-inner" @click="onClick"
  8. @onTouchStart="onTouchStart2($event, 1)" @touchmove="onTouchMove2($event, 1)"
  9. @touchend="onTouchEnd2($event, 1)" @touchcancel="onTouchEnd2($event, 1)"
  10. :class="[disabled ? 'u-slider--disabled' : '']" :style="{
  11. height: (isRange && showValue) ? (getPx(blockSize) + 24) + 'px' : (getPx(blockSize)) + 'px',
  12. }"
  13. >
  14. <view ref="u-slider__base"
  15. class="u-slider__base"
  16. :style="[
  17. {
  18. height: height,
  19. backgroundColor: inactiveColor
  20. }
  21. ]"
  22. >
  23. </view>
  24. <view
  25. @click="onClick"
  26. class="u-slider__gap"
  27. :style="[
  28. barStyle,
  29. {
  30. height: height,
  31. marginTop: '-' + height,
  32. backgroundColor: activeColor
  33. }
  34. ]"
  35. >
  36. </view>
  37. <view v-if="isRange"
  38. class="u-slider__gap u-slider__gap-0"
  39. :style="[
  40. barStyle0,
  41. {
  42. height: height,
  43. marginTop: '-' + height,
  44. backgroundColor: inactiveColor
  45. }
  46. ]"
  47. >
  48. </view>
  49. <text v-if="isRange && showValue"
  50. class="u-slider__show-range-value" :style="{left: (getPx(barStyle0.width) + getPx(blockSize)/2) + 'px'}">
  51. {{ this.rangeValue[0] }}
  52. </text>
  53. <text v-if="isRange && showValue"
  54. class="u-slider__show-range-value" :style="{left: (getPx(barStyle.width) + getPx(blockSize)/2) + 'px'}">
  55. {{ this.rangeValue[1] }}
  56. </text>
  57. <template v-if="isRange">
  58. <view class="u-slider__button-wrap u-slider__button-wrap-0" @touchstart="onTouchStart($event, 0)"
  59. @touchmove="onTouchMove($event, 0)" @touchend="onTouchEnd($event, 0)"
  60. @touchcancel="onTouchEnd($event, 0)" :style="{left: (getPx(barStyle0.width) + getPx(blockSize)/2) + 'px'}">
  61. <slot name="min" v-if="$slots.min || $slots.$min"/>
  62. <view v-else class="u-slider__button" :style="[blockStyle, {
  63. height: getPx(blockSize, true),
  64. width: getPx(blockSize, true),
  65. backgroundColor: blockColor
  66. }]"></view>
  67. </view>
  68. </template>
  69. <view class="u-slider__button-wrap" @touchstart="onTouchStart"
  70. @touchmove="onTouchMove" @touchend="onTouchEnd"
  71. @touchcancel="onTouchEnd" :style="{left: (getPx(barStyle.width) + getPx(blockSize)/2) + 'px'}">
  72. <slot name="max" v-if="isRange && ($slots.max || $slots.$max)"/>
  73. <slot v-else-if="$slots.default || $slots.$default"/>
  74. <view v-else class="u-slider__button" :style="[blockStyle, {
  75. height: getPx(blockSize, true),
  76. width: getPx(blockSize, true),
  77. backgroundColor: blockColor
  78. }]"></view>
  79. </view>
  80. </view>
  81. <view class="u-slider__show-value" v-if="showValue && !isRange">{{ modelValue }}</view>
  82. </template>
  83. <slider
  84. class="u-slider__native"
  85. v-else
  86. :min="min"
  87. :max="max"
  88. :step="step"
  89. :value="modelValue"
  90. :activeColor="activeColor"
  91. :backgroundColor="inactiveColor"
  92. :blockSize="getPx(blockSize)"
  93. :blockColor="blockColor"
  94. :showValue="showValue"
  95. :disabled="disabled"
  96. @changing="changingHandler"
  97. @change="changeHandler"
  98. ></slider>
  99. </view>
  100. </template>
  101. <script>
  102. import { props } from './props';
  103. import { mpMixin } from '../../libs/mixin/mpMixin';
  104. import { mixin } from '../../libs/mixin/mixin';
  105. import { addStyle, getPx, sleep } from '../../libs/function/index.js';
  106. // #ifdef APP-NVUE
  107. const dom = uni.requireNativePlugin('dom')
  108. // #endif
  109. /**
  110. * slider 滑块选择器
  111. * @tutorial https://uview-plus.jiangruyi.com/components/slider.html
  112. * @property {Number | String} value 滑块默认值(默认0)
  113. * @property {Number | String} min 最小值(默认0)
  114. * @property {Number | String} max 最大值(默认100)
  115. * @property {Number | String} step 步长(默认1)
  116. * @property {Number | String} blockWidth 滑块宽度,高等于宽(30)
  117. * @property {Number | String} height 滑块条高度,单位rpx(默认6)
  118. * @property {String} inactiveColor 底部条背景颜色(默认#c0c4cc)
  119. * @property {String} activeColor 底部选择部分的背景颜色(默认#2979ff)
  120. * @property {String} blockColor 滑块颜色(默认#ffffff)
  121. * @property {Object} blockStyle 给滑块自定义样式,对象形式
  122. * @property {Boolean} disabled 是否禁用滑块(默认为false)
  123. * @event {Function} changing 正在滑动中
  124. * @event {Function} change 滑动结束
  125. * @example <up-slider v-model="value" />
  126. */
  127. export default {
  128. name: 'u-slider',
  129. mixins: [mpMixin, mixin, props],
  130. emits: ["start", "changing", "change", "update:modelValue"],
  131. data() {
  132. return {
  133. startX: 0,
  134. status: 'end',
  135. newValue: 0,
  136. distanceX: 0,
  137. startValue0: 0,
  138. startValue: 0,
  139. barStyle0: {},
  140. barStyle: {},
  141. sliderRect: {
  142. left: 0,
  143. width: 0
  144. }
  145. };
  146. },
  147. watch: {
  148. // #ifdef VUE3
  149. modelValue(n) {
  150. // 只有在非滑动状态时,才可以通过value更新滑块值,这里监听,是为了让用户触发
  151. if (this.status == 'end') {
  152. const $crtFmtValue = this.updateValue(this.modelValue, false);
  153. this.$emit('change', $crtFmtValue);
  154. }
  155. },
  156. // #endif
  157. // #ifdef VUE2
  158. value(n) {
  159. // 只有在非滑动状态时,才可以通过value更新滑块值,这里监听,是为了让用户触发
  160. if (this.status == 'end') {
  161. const $crtFmtValue = this.updateValue(this.value, false);
  162. this.$emit('change', $crtFmtValue);
  163. }
  164. },
  165. // #endif
  166. rangeValue:{
  167. handler(n){
  168. if (this.status == 'end') {
  169. this.updateValue(this.rangeValue[0], false, 0);
  170. this.updateValue(this.rangeValue[1], false, 1);
  171. this.$emit('change', this.rangeValue);
  172. }
  173. },
  174. deep:true
  175. }
  176. },
  177. created() {
  178. },
  179. async mounted() {
  180. // 获取滑块条的尺寸信息
  181. if (!this.useNative) {
  182. // #ifndef APP-NVUE
  183. this.$uGetRect('.u-slider__base').then(rect => {
  184. this.sliderRect = rect;
  185. // console.log('sliderRect', this.sliderRect)
  186. if (this.sliderRect.width == 0) {
  187. console.info('如在弹窗等元素中使用,请使用v-if来显示滑块,否则无法计算长度。')
  188. }
  189. this.init()
  190. });
  191. // #endif
  192. // #ifdef APP-NVUE
  193. await sleep(30) // 不延迟会出现size获取都为0的问题
  194. const ref = this.$refs['u-slider__base']
  195. ref &&
  196. dom.getComponentRect(ref, (res) => {
  197. // console.log(res)
  198. this.sliderRect = {
  199. left: res.size.left,
  200. width: res.size.width
  201. };
  202. this.init()
  203. })
  204. // #endif
  205. }
  206. },
  207. methods: {
  208. addStyle,
  209. getPx,
  210. init() {
  211. if (this.isRange) {
  212. this.updateValue(this.rangeValue[0], false, 0);
  213. this.updateValue(this.rangeValue[1], false, 1);
  214. } else {
  215. // #ifdef VUE3
  216. this.updateValue(this.modelValue, false);
  217. // #endif
  218. // #ifdef VUE2
  219. this.updateValue(this.value, false);
  220. // #endif
  221. }
  222. },
  223. // native拖动过程中触发
  224. changingHandler(e) {
  225. const {
  226. value
  227. } = e.detail
  228. // 更新v-model的值
  229. // #ifdef VUE3
  230. this.$emit("update:modelValue", value);
  231. // #endif
  232. // #ifdef VUE2
  233. this.$emit("input", value);
  234. // #endif
  235. // 触发事件
  236. this.$emit('changing', value)
  237. },
  238. // native滑动结束时触发
  239. changeHandler(e) {
  240. const {
  241. value
  242. } = e.detail
  243. // 更新v-model的值
  244. // #ifdef VUE3
  245. this.$emit("update:modelValue", value);
  246. // #endif
  247. // #ifdef VUE2
  248. this.$emit("input", value);
  249. // #endif
  250. // 触发事件
  251. this.$emit('change', value);
  252. },
  253. onTouchStart(event, index = 1) {
  254. if (this.disabled) return;
  255. this.startX = 0;
  256. // 触摸点集
  257. let touches = event.touches[0];
  258. // 触摸点到屏幕左边的距离
  259. this.startX = touches.clientX;
  260. // 此处的this.modelValue虽为props值,但是通过$emit('update:modelValue')进行了修改
  261. if (this.isRange) {
  262. this.startValue0 = this.format(this.rangeValue[0], 0);
  263. this.startValue = this.format(this.rangeValue[1], 1);
  264. } else {
  265. // #ifdef VUE3
  266. this.startValue = this.format(this.modelValue);
  267. // #endif
  268. // #ifdef VUE2
  269. this.startValue = this.format(this.value);
  270. // #endif
  271. }
  272. // 标示当前的状态为开始触摸滑动
  273. this.status = 'start';
  274. let clientX = 0;
  275. // #ifndef APP-NVUE
  276. clientX = touches.clientX;
  277. // #endif
  278. // #ifdef APP-NVUE
  279. clientX = touches.screenX;
  280. // #endif
  281. this.distanceX = clientX - this.sliderRect.left;
  282. // 获得移动距离对整个滑块的值,此为带有多位小数的值,不能用此更新视图
  283. // 否则造成通信阻塞,需要每改变一个step值时修改一次视图
  284. this.newValue = ((this.distanceX / this.sliderRect.width) * (this.max - this.min)) + parseFloat(this.min);
  285. this.status = 'moving';
  286. // 发出moving事件
  287. let $crtFmtValue = this.updateValue(this.newValue, true, index);
  288. this.$emit('changing', $crtFmtValue);
  289. },
  290. onTouchMove(event, index = 1) {
  291. if (this.disabled) return;
  292. // 连续触摸的过程会一直触发本方法,但只有手指触发且移动了才被认为是拖动了,才发出事件
  293. // 触摸后第一次移动已经将status设置为moving状态,故触摸第二次移动不会触发本事件
  294. if (this.status == 'start') this.$emit('start');
  295. let touches = event.touches[0];
  296. // console.log('touchs', touches)
  297. // 滑块的左边不一定跟屏幕左边接壤,所以需要减去最外层父元素的左边值
  298. let clientX = 0;
  299. // #ifndef APP-NVUE
  300. clientX = touches.clientX;
  301. // #endif
  302. // #ifdef APP-NVUE
  303. clientX = touches.screenX;
  304. // #endif
  305. this.distanceX = clientX - this.sliderRect.left;
  306. // 获得移动距离对整个滑块的值,此为带有多位小数的值,不能用此更新视图
  307. // 否则造成通信阻塞,需要每改变一个step值时修改一次视图
  308. this.newValue = ((this.distanceX / this.sliderRect.width) * (this.max - this.min)) + parseFloat(this.min);
  309. this.status = 'moving';
  310. // 发出moving事件
  311. let $crtFmtValue = this.updateValue(this.newValue, true, index);
  312. this.$emit('changing', $crtFmtValue);
  313. },
  314. onTouchEnd(event, index = 1) {
  315. if (this.disabled) return;
  316. if (this.status === 'moving') {
  317. let $crtFmtValue = this.updateValue(this.newValue, false, index);
  318. this.$emit('change', $crtFmtValue);
  319. }
  320. this.status = 'end';
  321. },
  322. onTouchStart2(event, index = 1) {
  323. if (!this.isRange) {
  324. // this.onChangeStart(event, index);
  325. }
  326. },
  327. onTouchMove2(event, index = 1) {
  328. if (!this.isRange) {
  329. // this.onTouchMove(event, index);
  330. }
  331. },
  332. onTouchEnd2(event, index = 1) {
  333. if (!this.isRange) {
  334. // this.onTouchEnd(event, index);
  335. }
  336. },
  337. onClick(event) {
  338. // if (this.isRange) return;
  339. if (this.disabled) return;
  340. // 直接点击滑块的情况,计算方式与onTouchMove方法相同
  341. // console.log('click', event)
  342. // #ifndef APP-NVUE
  343. // nvue下暂时无法获取坐标
  344. let clientX = event.detail.x - this.sliderRect.left
  345. this.newValue = ((clientX / this.sliderRect.width) * (this.max - this.min)) + parseFloat(this.min);
  346. this.updateValue(this.newValue, false, 1);
  347. // #endif
  348. },
  349. updateValue(value, drag, index = 1) {
  350. // 去掉小数部分,同时也是对step步进的处理
  351. let valueFormat = this.format(value, index);
  352. // 不允许滑动的值超过max最大值
  353. if(valueFormat > this.max ) {
  354. valueFormat = this.max
  355. }
  356. // 设置移动的距离,不能用百分比,因为NVUE不支持。
  357. let width = Math.min((valueFormat - this.min) / (this.max - this.min) * this.sliderRect.width, this.sliderRect.width)
  358. let barStyle = {
  359. width: width + 'px'
  360. };
  361. // 移动期间无需过渡动画
  362. if (drag == true) {
  363. barStyle.transition = 'none';
  364. } else {
  365. // 非移动期间,删掉对过渡为空的声明,让css中的声明起效
  366. delete barStyle.transition;
  367. }
  368. // 修改value值
  369. if (this.isRange) {
  370. this.rangeValue[index] = valueFormat;
  371. this.$emit("update:modelValue", this.rangeValue);
  372. } else {
  373. // #ifdef VUE3
  374. this.$emit("update:modelValue", valueFormat);
  375. // #endif
  376. // #ifdef VUE2
  377. this.$emit("input", valueFormat);
  378. // #endif
  379. }
  380. switch (index) {
  381. case 0:
  382. this.barStyle0 = {...barStyle};
  383. break;
  384. case 1:
  385. this.barStyle = {...barStyle};
  386. break;
  387. default:
  388. break;
  389. }
  390. if (this.isRange) {
  391. return this.rangeValue
  392. } else {
  393. return valueFormat
  394. }
  395. },
  396. format(value, index = 1) {
  397. // 将小数变成整数,为了减少对视图的更新,造成视图层与逻辑层的阻塞
  398. if (this.isRange) {
  399. switch (index) {
  400. case 0:
  401. return Math.round(
  402. Math.max(this.min, Math.min(value, this.rangeValue[1] - parseInt(this.step),this.max))
  403. / parseInt(this.step)
  404. ) * parseInt(this.step);
  405. break;
  406. case 1:
  407. return Math.round(
  408. Math.max(this.min, this.rangeValue[0] + parseInt(this.step), Math.min(value, this.max))
  409. / parseInt(this.step)
  410. ) * parseInt(this.step);
  411. break;
  412. default:
  413. break;
  414. }
  415. } else {
  416. return Math.round(
  417. Math.max(this.min, Math.min(value, this.max))
  418. / parseInt(this.step)
  419. ) * parseInt(this.step);
  420. }
  421. }
  422. }
  423. }
  424. </script>
  425. <style lang="scss" scoped>
  426. .u-slider {
  427. position: relative;
  428. display: flex;
  429. flex-direction: row;
  430. align-items: center;
  431. &__native {
  432. flex: 1;
  433. }
  434. &-inner {
  435. flex: 1;
  436. display: flex;
  437. flex-direction: column;
  438. position: relative;
  439. border-radius: 999px;
  440. padding: 10px 18px;
  441. justify-content: center;
  442. }
  443. &__show-value {
  444. margin: 10px 18px 10px 0px;
  445. }
  446. &__show-range-value {
  447. padding-top: 2px;
  448. font-size: 12px;
  449. line-height: 12px;
  450. position: absolute;
  451. bottom: 0;
  452. }
  453. &__base {
  454. background-color: #ebedf0;
  455. }
  456. /* #ifndef APP-NVUE */
  457. &-inner:before {
  458. position: absolute;
  459. right: 0;
  460. left: 0;
  461. content: '';
  462. top: -8px;
  463. bottom: -8px;
  464. z-index: -1;
  465. }
  466. /* #endif */
  467. &__gap {
  468. position: relative;
  469. border-radius: 999px;
  470. transition: width 0.2s;
  471. background-color: #1989fa;
  472. }
  473. &__button {
  474. width: 24px;
  475. height: 24px;
  476. border-radius: 50%;
  477. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  478. background-color: #fff;
  479. transform: scale(0.9);
  480. /* #ifdef H5 */
  481. cursor: pointer;
  482. /* #endif */
  483. }
  484. &__button-wrap {
  485. position: absolute;
  486. // transform: translate3d(50%, -50%, 0);
  487. }
  488. &--disabled {
  489. opacity: 0.5;
  490. }
  491. }
  492. </style>