FontSizeSlider.vue 8.1 KB

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