u-slider.vue 15 KB

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