u-slider.vue 14 KB

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