u-slider.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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') this.updateValue(this.modelValue, false);
  152. },
  153. // #endif
  154. // #ifdef VUE2
  155. value(n) {
  156. // 只有在非滑动状态时,才可以通过value更新滑块值,这里监听,是为了让用户触发
  157. if(this.status == 'end') this.updateValue(this.value, false);
  158. },
  159. // #endif
  160. rangeValue:{
  161. handler(n){
  162. if(this.status == 'end'){
  163. this.updateValue(this.rangeValue[0], false, 0);
  164. this.updateValue(this.rangeValue[1], false, 1);
  165. }
  166. },
  167. deep:true
  168. }
  169. },
  170. created() {
  171. },
  172. async mounted() {
  173. // 获取滑块条的尺寸信息
  174. if (!this.useNative) {
  175. // #ifndef APP-NVUE
  176. this.$uGetRect('.u-slider__base').then(rect => {
  177. this.sliderRect = rect;
  178. // console.log('sliderRect', this.sliderRect)
  179. if (this.sliderRect.width == 0) {
  180. console.info('如在弹窗等元素中使用,请使用v-if来显示滑块,否则无法计算长度。')
  181. }
  182. this.init()
  183. });
  184. // #endif
  185. // #ifdef APP-NVUE
  186. await sleep(30) // 不延迟会出现size获取都为0的问题
  187. const ref = this.$refs['u-slider__base']
  188. ref &&
  189. dom.getComponentRect(ref, (res) => {
  190. // console.log(res)
  191. this.sliderRect = {
  192. left: res.size.left,
  193. width: res.size.width
  194. };
  195. this.init()
  196. })
  197. // #endif
  198. }
  199. },
  200. methods: {
  201. addStyle,
  202. getPx,
  203. init() {
  204. if (this.isRange) {
  205. this.updateValue(this.rangeValue[0], false, 0);
  206. this.updateValue(this.rangeValue[1], false, 1);
  207. } else {
  208. // #ifdef VUE3
  209. this.updateValue(this.modelValue, false);
  210. // #endif
  211. // #ifdef VUE2
  212. this.updateValue(this.value, false);
  213. // #endif
  214. }
  215. },
  216. // native拖动过程中触发
  217. changingHandler(e) {
  218. const {
  219. value
  220. } = e.detail
  221. // 更新v-model的值
  222. // #ifdef VUE3
  223. this.$emit("update:modelValue", value);
  224. // #endif
  225. // #ifdef VUE2
  226. this.$emit("input", value);
  227. // #endif
  228. // 触发事件
  229. this.$emit('changing', value)
  230. },
  231. // native滑动结束时触发
  232. changeHandler(e) {
  233. const {
  234. value
  235. } = e.detail
  236. // 更新v-model的值
  237. // #ifdef VUE3
  238. this.$emit("update:modelValue", value);
  239. // #endif
  240. // #ifdef VUE2
  241. this.$emit("input", value);
  242. // #endif
  243. // 触发事件
  244. this.$emit('change', value);
  245. },
  246. onTouchStart(event, index = 1) {
  247. if (this.disabled) return;
  248. this.startX = 0;
  249. // 触摸点集
  250. let touches = event.touches[0];
  251. // 触摸点到屏幕左边的距离
  252. this.startX = touches.clientX;
  253. // 此处的this.modelValue虽为props值,但是通过$emit('update:modelValue')进行了修改
  254. if (this.isRange) {
  255. this.startValue0 = this.format(this.rangeValue[0], 0);
  256. this.startValue = this.format(this.rangeValue[1], 1);
  257. } else {
  258. // #ifdef VUE3
  259. this.startValue = this.format(this.modelValue);
  260. // #endif
  261. // #ifdef VUE2
  262. this.startValue = this.format(this.value);
  263. // #endif
  264. }
  265. // 标示当前的状态为开始触摸滑动
  266. this.status = 'start';
  267. let clientX = 0;
  268. // #ifndef APP-NVUE
  269. clientX = touches.clientX;
  270. // #endif
  271. // #ifdef APP-NVUE
  272. clientX = touches.screenX;
  273. // #endif
  274. this.distanceX = clientX - this.sliderRect.left;
  275. // 获得移动距离对整个滑块的值,此为带有多位小数的值,不能用此更新视图
  276. // 否则造成通信阻塞,需要每改变一个step值时修改一次视图
  277. this.newValue = ((this.distanceX / this.sliderRect.width) * (this.max - this.min)) + parseFloat(this.min);
  278. this.status = 'moving';
  279. // 发出moving事件
  280. let $crtFmtValue = this.updateValue(this.newValue, true, index);
  281. this.$emit('changing', $crtFmtValue);
  282. },
  283. onTouchMove(event, index = 1) {
  284. if (this.disabled) return;
  285. // 连续触摸的过程会一直触发本方法,但只有手指触发且移动了才被认为是拖动了,才发出事件
  286. // 触摸后第一次移动已经将status设置为moving状态,故触摸第二次移动不会触发本事件
  287. if (this.status == 'start') this.$emit('start');
  288. let touches = event.touches[0];
  289. // console.log('touchs', touches)
  290. // 滑块的左边不一定跟屏幕左边接壤,所以需要减去最外层父元素的左边值
  291. let clientX = 0;
  292. // #ifndef APP-NVUE
  293. clientX = touches.clientX;
  294. // #endif
  295. // #ifdef APP-NVUE
  296. clientX = touches.screenX;
  297. // #endif
  298. this.distanceX = clientX - this.sliderRect.left;
  299. // 获得移动距离对整个滑块的值,此为带有多位小数的值,不能用此更新视图
  300. // 否则造成通信阻塞,需要每改变一个step值时修改一次视图
  301. this.newValue = ((this.distanceX / this.sliderRect.width) * (this.max - this.min)) + parseFloat(this.min);
  302. this.status = 'moving';
  303. // 发出moving事件
  304. let $crtFmtValue = this.updateValue(this.newValue, true, index);
  305. this.$emit('changing', $crtFmtValue);
  306. },
  307. onTouchEnd(event, index = 1) {
  308. if (this.disabled) return;
  309. if (this.status === 'moving') {
  310. let $crtFmtValue = this.updateValue(this.newValue, false, index);
  311. this.$emit('change', $crtFmtValue);
  312. }
  313. this.status = 'end';
  314. },
  315. onTouchStart2(event, index = 1) {
  316. if (!this.isRange) {
  317. // this.onChangeStart(event, index);
  318. }
  319. },
  320. onTouchMove2(event, index = 1) {
  321. if (!this.isRange) {
  322. // this.onTouchMove(event, index);
  323. }
  324. },
  325. onTouchEnd2(event, index = 1) {
  326. if (!this.isRange) {
  327. // this.onTouchEnd(event, index);
  328. }
  329. },
  330. onClick(event) {
  331. // if (this.isRange) return;
  332. if (this.disabled) return;
  333. // 直接点击滑块的情况,计算方式与onTouchMove方法相同
  334. // console.log('click', event)
  335. // #ifndef APP-NVUE
  336. // nvue下暂时无法获取坐标
  337. let clientX = event.detail.x - this.sliderRect.left
  338. this.newValue = ((clientX / this.sliderRect.width) * (this.max - this.min)) + parseFloat(this.min);
  339. this.updateValue(this.newValue, false, 1);
  340. // #endif
  341. },
  342. updateValue(value, drag, index = 1) {
  343. // 去掉小数部分,同时也是对step步进的处理
  344. let valueFormat = this.format(value, index);
  345. // 不允许滑动的值超过max最大值
  346. if(valueFormat > this.max ) {
  347. valueFormat = this.max
  348. }
  349. // 设置移动的距离,不能用百分比,因为NVUE不支持。
  350. let width = Math.min((valueFormat - this.min) / (this.max - this.min) * this.sliderRect.width, this.sliderRect.width)
  351. let barStyle = {
  352. width: width + 'px'
  353. };
  354. // 移动期间无需过渡动画
  355. if (drag == true) {
  356. barStyle.transition = 'none';
  357. } else {
  358. // 非移动期间,删掉对过渡为空的声明,让css中的声明起效
  359. delete barStyle.transition;
  360. }
  361. // 修改value值
  362. if (this.isRange) {
  363. this.rangeValue[index] = valueFormat;
  364. this.$emit("update:modelValue", this.rangeValue);
  365. } else {
  366. // #ifdef VUE3
  367. this.$emit("update:modelValue", valueFormat);
  368. // #endif
  369. // #ifdef VUE2
  370. this.$emit("input", valueFormat);
  371. // #endif
  372. }
  373. switch (index) {
  374. case 0:
  375. this.barStyle0 = {...barStyle};
  376. break;
  377. case 1:
  378. this.barStyle = {...barStyle};
  379. break;
  380. default:
  381. break;
  382. }
  383. if (this.isRange) {
  384. return this.rangeValue
  385. } else {
  386. return valueFormat
  387. }
  388. },
  389. format(value, index = 1) {
  390. // 将小数变成整数,为了减少对视图的更新,造成视图层与逻辑层的阻塞
  391. if (this.isRange) {
  392. switch (index) {
  393. case 0:
  394. return Math.round(
  395. Math.max(this.min, Math.min(value, this.rangeValue[1] - parseInt(this.step),this.max))
  396. / parseInt(this.step)
  397. ) * parseInt(this.step);
  398. break;
  399. case 1:
  400. return Math.round(
  401. Math.max(this.min, this.rangeValue[0] + parseInt(this.step), Math.min(value, this.max))
  402. / parseInt(this.step)
  403. ) * parseInt(this.step);
  404. break;
  405. default:
  406. break;
  407. }
  408. } else {
  409. return Math.round(
  410. Math.max(this.min, Math.min(value, this.max))
  411. / parseInt(this.step)
  412. ) * parseInt(this.step);
  413. }
  414. }
  415. }
  416. }
  417. </script>
  418. <style lang="scss" scoped>
  419. @import "../../libs/css/components.scss";
  420. .u-slider {
  421. position: relative;
  422. display: flex;
  423. flex-direction: row;
  424. align-items: center;
  425. &__native {
  426. flex: 1;
  427. }
  428. &-inner {
  429. flex: 1;
  430. display: flex;
  431. flex-direction: column;
  432. position: relative;
  433. border-radius: 999px;
  434. padding: 10px 18px;
  435. justify-content: center;
  436. }
  437. &__show-value {
  438. margin: 10px 18px 10px 0px;
  439. }
  440. &__show-range-value {
  441. padding-top: 2px;
  442. font-size: 12px;
  443. line-height: 12px;
  444. position: absolute;
  445. bottom: 0;
  446. }
  447. &__base {
  448. background-color: #ebedf0;
  449. }
  450. /* #ifndef APP-NVUE */
  451. &-inner:before {
  452. position: absolute;
  453. right: 0;
  454. left: 0;
  455. content: '';
  456. top: -8px;
  457. bottom: -8px;
  458. z-index: -1;
  459. }
  460. /* #endif */
  461. &__gap {
  462. position: relative;
  463. border-radius: 999px;
  464. transition: width 0.2s;
  465. background-color: #1989fa;
  466. }
  467. &__button {
  468. width: 24px;
  469. height: 24px;
  470. border-radius: 50%;
  471. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  472. background-color: #fff;
  473. transform: scale(0.9);
  474. /* #ifdef H5 */
  475. cursor: pointer;
  476. /* #endif */
  477. }
  478. &__button-wrap {
  479. position: absolute;
  480. // transform: translate3d(50%, -50%, 0);
  481. }
  482. &--disabled {
  483. opacity: 0.5;
  484. }
  485. }
  486. </style>