u-slider.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <view class="u-slider" @tap="onClick" :class="[disabled ? 'u-slider--disabled' : '']" :style="{
  3. backgroundColor: inactiveColor
  4. }">
  5. <view
  6. class="u-slider__gap"
  7. :style="[
  8. barStyle,
  9. {
  10. height: height + 'rpx',
  11. backgroundColor: activeColor
  12. }
  13. ]"
  14. >
  15. <view class="u-slider__button-wrap" @touchstart="onTouchStart"
  16. @touchmove="onTouchMove" @touchend="onTouchEnd"
  17. @touchcancel="onTouchEnd">
  18. <slot v-if="$slots.default || $slots.$default"/>
  19. <view v-else class="u-slider__button" :style="[blockStyle, {
  20. height: blockWidth + 'rpx',
  21. width: blockWidth + 'rpx',
  22. backgroundColor: blockColor
  23. }]"></view>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. /**
  30. * slider 滑块选择器
  31. * @tutorial https://uviewui.com/components/slider.html
  32. * @property {Number | String} value 滑块默认值(默认0)
  33. * @property {Number | String} min 最小值(默认0)
  34. * @property {Number | String} max 最大值(默认100)
  35. * @property {Number | String} step 步长(默认1)
  36. * @property {Number | String} blockWidth 滑块宽度,高等于宽(30)
  37. * @property {Number | String} height 滑块条高度,单位rpx(默认6)
  38. * @property {String} inactiveColor 底部条背景颜色(默认#c0c4cc)
  39. * @property {String} activeColor 底部选择部分的背景颜色(默认#2979ff)
  40. * @property {String} blockColor 滑块颜色(默认#ffffff)
  41. * @property {Object} blockStyle 给滑块自定义样式,对象形式
  42. * @property {Boolean} disabled 是否禁用滑块(默认为false)
  43. * @event {Function} start 滑动触发
  44. * @event {Function} moving 正在滑动中
  45. * @event {Function} end 滑动结束
  46. * @example <u-slider v-model="value" />
  47. */
  48. export default {
  49. name: 'u-slider',
  50. emits: ["update:modelValue", "input", "start", "moving", "end"],
  51. props: {
  52. // 当前进度百分比值,范围0-100
  53. value: {
  54. type: [Number, String],
  55. default: 0
  56. },
  57. modelValue: {
  58. type: [Number, String],
  59. default: 0
  60. },
  61. // 是否禁用滑块
  62. disabled: {
  63. type: Boolean,
  64. default: false
  65. },
  66. // 滑块宽度,高等于宽,单位rpx
  67. blockWidth: {
  68. type: [Number, String],
  69. default: 30
  70. },
  71. // 最小值
  72. min: {
  73. type: [Number, String],
  74. default: 0
  75. },
  76. // 最大值
  77. max: {
  78. type: [Number, String],
  79. default: 100
  80. },
  81. // 步进值
  82. step: {
  83. type: [Number, String],
  84. default: 1
  85. },
  86. // 滑块条高度,单位rpx
  87. height: {
  88. type: [Number, String],
  89. default: 6
  90. },
  91. // 进度条的激活部分颜色
  92. activeColor: {
  93. type: String,
  94. default: '#2979ff'
  95. },
  96. // 进度条的背景颜色
  97. inactiveColor: {
  98. type: String,
  99. default: '#c0c4cc'
  100. },
  101. // 滑块的背景颜色
  102. blockColor: {
  103. type: String,
  104. default: '#ffffff'
  105. },
  106. // 用户对滑块的自定义颜色
  107. blockStyle: {
  108. type: Object,
  109. default() {
  110. return {};
  111. }
  112. },
  113. },
  114. data() {
  115. return {
  116. startX: 0,
  117. status: 'end',
  118. newValue: 0,
  119. distanceX: 0,
  120. startValue: 0,
  121. barStyle: {},
  122. sliderRect: {
  123. left: 0,
  124. width: 0
  125. }
  126. };
  127. },
  128. watch: {
  129. value(n) {
  130. // 只有在非滑动状态时,才可以通过value更新滑块值,这里监听,是为了让用户触发
  131. if(this.status == 'end') this.updateValue(this.getValue(), false);
  132. },
  133. modelValue(n) {
  134. // 只有在非滑动状态时,才可以通过value更新滑块值,这里监听,是为了让用户触发
  135. if(this.status == 'end') this.updateValue(this.getValue(), false);
  136. },
  137. },
  138. created() {
  139. this.updateValue(this.getValue(), false);
  140. },
  141. mounted() {
  142. // 获取滑块条的尺寸信息
  143. this.$uGetRect('.u-slider').then(rect => {
  144. this.sliderRect = rect;
  145. });
  146. },
  147. methods: {
  148. getValue(){
  149. // #ifndef VUE3
  150. return this.value;
  151. // #endif
  152. // #ifdef VUE3
  153. return this.modelValue;
  154. // #endif
  155. },
  156. onTouchStart(event) {
  157. if (this.disabled) return;
  158. this.startX = 0;
  159. // 触摸点集
  160. let touches = event.touches[0];
  161. // 触摸点到屏幕左边的距离
  162. this.startX = touches.clientX;
  163. // 此处的this.value虽为props值,但是通过$emit('input')进行了修改
  164. this.startValue = this.format(this.getValue());
  165. // 标示当前的状态为开始触摸滑动
  166. this.status = 'start';
  167. },
  168. onTouchMove(event) {
  169. if (this.disabled) return;
  170. // 连续触摸的过程会一直触发本方法,但只有手指触发且移动了才被认为是拖动了,才发出事件
  171. // 触摸后第一次移动已经将status设置为moving状态,故触摸第二次移动不会触发本事件
  172. if (this.status == 'start') this.$emit('start');
  173. let touches = event.touches[0];
  174. // 滑块的左边不一定跟屏幕左边接壤,所以需要减去最外层父元素的左边值
  175. this.distanceX = touches.clientX - this.sliderRect.left;
  176. // 获得移动距离对整个滑块的百分比值,此为带有多位小数的值,不能用此更新视图
  177. // 否则造成通信阻塞,需要每改变一个step值时修改一次视图
  178. this.newValue = (this.distanceX / this.sliderRect.width) * 100;
  179. this.status = 'moving';
  180. // 发出moving事件
  181. this.$emit('moving');
  182. this.updateValue(this.newValue, true);
  183. },
  184. onTouchEnd() {
  185. if (this.disabled) return;
  186. if (this.status === 'moving') {
  187. this.updateValue(this.newValue, false);
  188. this.$emit('end');
  189. }
  190. this.status = 'end';
  191. },
  192. updateValue(value, drag) {
  193. // 去掉小数部分,同时也是对step步进的处理
  194. const width = this.format(value);
  195. // 不允许滑动的值超过max最大值,百分比也不能超过100
  196. if(width > this.max || width > 100) return;
  197. // 设置移动的百分比值
  198. let barStyle = {
  199. width: width + '%'
  200. };
  201. // 移动期间无需过渡动画
  202. if (drag == true) {
  203. barStyle.transition = 'none';
  204. } else {
  205. // 非移动期间,删掉对过渡为空的声明,让css中的声明起效
  206. delete barStyle.transition;
  207. }
  208. // 修改value值
  209. this.$emit('input', width);
  210. this.$emit("update:modelValue", width);
  211. this.barStyle = barStyle;
  212. },
  213. format(value) {
  214. // 将小数变成整数,为了减少对视图的更新,造成视图层与逻辑层的阻塞
  215. return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step;
  216. },
  217. onClick(event) {
  218. if (this.disabled) return;
  219. // 直接点击滑块的情况,计算方式与onTouchMove方法相同
  220. const value = ((event.detail.x - this.sliderRect.left) / this.sliderRect.width) * 100;
  221. this.updateValue(value, false);
  222. }
  223. }
  224. };
  225. </script>
  226. <style lang="scss" scoped>
  227. @import "../../libs/css/style.components.scss";
  228. .u-slider {
  229. position: relative;
  230. border-radius: 999px;
  231. border-radius: 999px;
  232. background-color: #ebedf0;
  233. }
  234. .u-slider:before {
  235. position: absolute;
  236. right: 0;
  237. left: 0;
  238. content: '';
  239. top: -8px;
  240. bottom: -8px;
  241. z-index: -1;
  242. }
  243. .u-slider__gap {
  244. position: relative;
  245. border-radius: inherit;
  246. transition: width 0.2s;
  247. transition: width 0.2s;
  248. background-color: #1989fa;
  249. }
  250. .u-slider__button {
  251. width: 24px;
  252. height: 24px;
  253. border-radius: 50%;
  254. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  255. background-color: #fff;
  256. cursor: pointer;
  257. }
  258. .u-slider__button-wrap {
  259. position: absolute;
  260. top: 50%;
  261. right: 0;
  262. transform: translate3d(50%, -50%, 0);
  263. }
  264. .u-slider--disabled {
  265. opacity: 0.5;
  266. }
  267. </style>