u-slider.vue 14 KB

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