cj-slider.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <view class="cj-slider" @mousemove="onSliMouseMove" @mouseup="onSliMouseUp">
  3. <!-- <view class="mouse-move" :style="{height: moveHeight + 'rpx'}"></view> -->
  4. <view class="cj-slider__line" :class="[disabled ? 'cj-slider--disabled' : '']" :style="{
  5. backgroundColor: inactiveColor,
  6. height: height + 'rpx'
  7. }" @click="onClick"></view>
  8. <view class="cj-slider__gap" :style="[
  9. barStyle,
  10. {
  11. height: height + 'rpx',
  12. backgroundColor: activeColor
  13. }
  14. ]" @click="onClick">
  15. <view class="cj-slider__button-wrap" @mousedown="onMouseDown(1, $event)" @mousemove="onMouseMove($event)"
  16. @mouseleave="onMouseLeave(1)" @mouseup="onMouseUp(1)" @touchstart="onTouchStart(1, $event)"
  17. @touchmove="onTouchMove(1, $event)" @touchend="onTouchEnd(1)" @touchcancel="onTouchEnd">
  18. <slot v-if="$slots.default || $slots.$default" />
  19. <view v-else class="cj-slider__button" :style="[blockStyle, {
  20. height: blockWidth + 'rpx',
  21. width: blockWidth + 'rpx',
  22. backgroundColor: blockColor
  23. }]"></view>
  24. </view>
  25. <view class="cj-slider__button-wrap2" @mousedown="onMouseDown(2, $event)" @mousemove="onMouseMove($event)"
  26. @mouseleave="onMouseLeave(2)" @mouseup="onMouseUp(2)" @touchstart="onTouchStart(2, $event)"
  27. @touchmove="onTouchMove(2, $event)" @touchend="onTouchEnd(2)" @touchcancel="onTouchEnd">
  28. <slot v-if="$slots.default || $slots.$default" />
  29. <view v-else class="cj-slider__button" :style="[blockStyle, {
  30. height: blockWidth + 'rpx',
  31. width: blockWidth + 'rpx',
  32. backgroundColor: blockColor
  33. }]"></view>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. /**
  40. * slider 滑块选择器
  41. * @property {Number | String} value 滑块默认值(默认[最小值,最大值])
  42. * @property {Number | String} min 最小值(默认0)
  43. * @property {Number | String} max 最大值(默认100)
  44. * @property {Number | String} step 步长(默认1)
  45. * @property {Number | String} blockWidth 滑块宽度,高等于宽(30)
  46. * @property {Number | String} height 滑块条高度,单位rpx(默认6)
  47. * @property {String} inactiveColor 底部条背景颜色(默认#c0c4cc)
  48. * @property {String} activeColor 底部选择部分的背景颜色(默认#2979ff)
  49. * @property {String} blockColor 滑块颜色(默认#ffffff)
  50. * @property {Object} blockStyle 给滑块自定义样式,对象形式
  51. * @property {Boolean} disabled 是否禁用滑块(默认为false)
  52. * @property {Number | String} moveHeight 鼠标离开滑块仍可滑动的区域高度,单位rpx(默认100)
  53. * @event {Function} start 滑动触发
  54. * @event {Function} moving 正在滑动中
  55. * @event {Function} end 滑动结束
  56. * @example <cj-slider v-model="value" />
  57. */
  58. export default {
  59. name: 'cj-slider',
  60. props: {
  61. // 当前进度百分比值
  62. value: {
  63. type: Array,
  64. default: []
  65. },
  66. // 是否禁用滑块
  67. disabled: {
  68. type: Boolean,
  69. default: false
  70. },
  71. // 滑块宽度,高等于宽,单位rpx
  72. blockWidth: {
  73. type: [Number, String],
  74. default: 30
  75. },
  76. // 最小值
  77. min: {
  78. type: [Number, String],
  79. default: 0
  80. },
  81. // 最大值
  82. max: {
  83. type: [Number, String],
  84. default: 100
  85. },
  86. // 步进值
  87. step: {
  88. type: [Number, String],
  89. default: 1
  90. },
  91. // 滑块条高度,单位rpx
  92. height: {
  93. type: [Number, String],
  94. default: 6
  95. },
  96. // 进度条的激活部分颜色
  97. activeColor: {
  98. type: String,
  99. default: '#2979ff'
  100. },
  101. // 进度条的背景颜色
  102. inactiveColor: {
  103. type: String,
  104. default: '#c0c4cc'
  105. },
  106. // 滑块的背景颜色
  107. blockColor: {
  108. type: String,
  109. default: '#ffffff'
  110. },
  111. // 用户对滑块的自定义颜色
  112. blockStyle: {
  113. type: Object,
  114. default () {
  115. return {};
  116. }
  117. },
  118. // 鼠标可离开滑块后仍能滑动的范围高度
  119. moveHeight: {
  120. type: [Number, String],
  121. default: '500'
  122. }
  123. },
  124. data() {
  125. return {
  126. mouseLeave: false, // 鼠标移出了滑块
  127. mouseType: 1, // 1 左滑块 2 右滑块
  128. mouseDown: false, // 鼠标按下
  129. startX: 0,
  130. status: 'end',
  131. newValue: 0,
  132. distanceX: 0,
  133. startValue: 0,
  134. barStyle: {},
  135. sliderRect: {
  136. left: 0,
  137. width: 0
  138. }
  139. };
  140. },
  141. created() {
  142. // 如果不是长度为2的数组,默认值为[最小值, 最大值]
  143. if (Object.prototype.toString.call(this.value) == '[object Array]' && this.value.length === 2) {
  144. this.updateValue(this.value[0], this.value[1], false);
  145. } else {
  146. this.$emit('input', Array(this.min, this.max))
  147. }
  148. },
  149. mounted() {
  150. // 获取滑块条的尺寸信息
  151. this.$uGetRect('.cj-slider').then(rect => {
  152. this.sliderRect = rect;
  153. });
  154. },
  155. methods: {
  156. onTouchStart(type, 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 = type === 1 ? this.format(this.value[0]) : this.format(this.value[1]);
  165. // 标示当前的状态为开始触摸滑动
  166. this.status = 'start';
  167. },
  168. onMouseDown(type, event) {
  169. if (this.disabled) return;
  170. this.mouseDown = true;
  171. this.mouseType = type
  172. this.startX = event.clientX || 0;
  173. this.startValue = type === 1 ? this.format(this.value[0]) : this.format(this.value[1]);
  174. this.status = 'start';
  175. },
  176. onTouchMove(type, event) {
  177. if (this.disabled) return;
  178. // 连续触摸的过程会一直触发本方法,但只有手指触发且移动了才被认为是拖动了,才发出事件
  179. // 触摸后第一次移动已经将status设置为moving状态,故触摸第二次移动不会触发本事件
  180. if (this.status == 'start') this.$emit('start');
  181. let touches = event.touches[0]
  182. // 滑块的左边不一定跟屏幕左边接壤,所以需要减去最外层父元素的左边值
  183. this.distanceX = touches.clientX - this.sliderRect.left
  184. // 获得移动距离对整个滑块的百分比值,此为带有多位小数的值,不能用此更新视图
  185. // 否则造成通信阻塞,需要每改变一个step值时修改一次视图
  186. this.newValue = (this.distanceX / this.sliderRect.width) * (this.max - this.min) + this.min
  187. this.status = 'moving'
  188. // 发出moving事件
  189. this.$emit('moving');
  190. if (type === 1) {
  191. this.updateValue(this.newValue, this.format(this.value[1]), true);
  192. } else {
  193. this.updateValue(this.format(this.value[0]), this.newValue, true);
  194. }
  195. },
  196. onMouseMove(event) {
  197. if (!this.mouseDown) return;
  198. if (this.disabled) return;
  199. if (this.status == 'start') this.$emit('start');
  200. // 滑块的左边不一定跟屏幕左边接壤,所以需要减去最外层父元素的左边值
  201. this.distanceX = event.clientX - this.sliderRect.left
  202. // 获得移动距离对整个滑块的百分比值,此为带有多位小数的值,不能用此更新视图
  203. // 否则造成通信阻塞,需要每改变一个step值时修改一次视图
  204. this.newValue = (this.distanceX / this.sliderRect.width) * (this.max - this.min) + this.min
  205. this.status = 'moving'
  206. // 发出moving事件
  207. this.$emit('moving');
  208. if (this.mouseType === 1) {
  209. this.updateValue(this.newValue, this.format(this.value[1]), true);
  210. } else {
  211. this.updateValue(this.format(this.value[0]), this.newValue, true);
  212. }
  213. },
  214. onMouseLeave(type) {
  215. this.mouseLeave = true
  216. },
  217. onTouchEnd(type) {
  218. if (this.disabled) return;
  219. if (this.status === 'moving') {
  220. if (type === 1) {
  221. this.updateValue(this.newValue, this.format(this.value[1]), true);
  222. } else {
  223. this.updateValue(this.format(this.value[0]), this.newValue, true);
  224. }
  225. this.$emit('end');
  226. }
  227. this.status = 'end';
  228. },
  229. onMouseUp(type) {
  230. this.mouseDown = false;
  231. this.mouseLeave = false;
  232. if (this.disabled) return;
  233. if (this.status === 'moving') {
  234. if (type === 1) {
  235. this.updateValue(this.newValue, this.format(this.value[1]), true);
  236. } else {
  237. this.updateValue(this.format(this.value[0]), this.newValue, true);
  238. }
  239. this.$emit('end');
  240. }
  241. this.status = 'end';
  242. },
  243. onSliMouseUp() {
  244. // 鼠标在滑块范围内松开
  245. this.mouseDown = false;
  246. this.mouseLeave = false;
  247. },
  248. onSliMouseMove(e) {
  249. // 监听整个滑动内的鼠标移动,因为PC端只能监听到小滑块内的移动,移动过快鼠标移出了小滑块则移动失败。
  250. if (!this.mouseDown) return;
  251. if (!this.mouseLeave) return;
  252. this.onMouseMove(e)
  253. },
  254. updateValue(value, value2, drag) {
  255. // 去掉小数部分,同时也是对step步进的处理
  256. const widthB1 = this.format(value)
  257. const widthB2 = this.format(value2)
  258. const widthB1B = Math.round((widthB1 - this.min) * 100 / (this.max - this.min))
  259. const widthB2B = Math.round((widthB2 - this.min) * 100 / (this.max - this.min))
  260. // 不允许滑动的值超过max最大值,百分比也不能超过100
  261. if (widthB1 > widthB2 || widthB2 > this.max || widthB2B > 100) return;
  262. // 设置移动的百分比值
  263. let barStyle = {
  264. width: widthB2B - widthB1B + '%',
  265. left: widthB1B + '%',
  266. };
  267. // 移动期间无需过渡动画
  268. if (drag == true) {
  269. barStyle.transition = 'none';
  270. } else {
  271. // 非移动期间,删掉对过渡为空的声明,让css中的声明起效
  272. delete barStyle.transition;
  273. }
  274. // 修改value值,这里使用change改变,使用input H5桌面端会卡死,
  275. this.$emit('input', Array(widthB1, widthB2));
  276. this.barStyle = barStyle;
  277. },
  278. format(value) {
  279. // 将小数变成整数,为了减少对视图的更新,造成视图层与逻辑层的阻塞
  280. return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step;
  281. },
  282. onClick(event) {
  283. if (this.disabled) return;
  284. // 直接点击滑块的情况,计算方式与onTouchMove方法相同
  285. const widthB1 = this.value[0]
  286. const widthB2 = this.value[1]
  287. const value = this.format(((event.detail.x - this.sliderRect.left) / this.sliderRect.width) * (this.max - this
  288. .min) +
  289. this.min);
  290. if (value < widthB1 || (value - widthB1) <= (widthB2 - value)) {
  291. // 点击位置在左滑块的左边 || 点击位置在中间,且靠近左滑块 => 移动左滑块到该位置
  292. this.updateValue(value, widthB2, false)
  293. } else {
  294. // 点击位置在右滑块的右边 || 点击位置在中间,且靠近右滑块 => 移动右滑块到该位置
  295. this.updateValue(widthB1, value, false)
  296. }
  297. },
  298. $uGetRect(selector, all) {
  299. // $uGetRect为uView自带的节点查询简化方法
  300. return new Promise(resolve => {
  301. uni.createSelectorQuery().
  302. in(this)[all ? 'selectAll' : 'select'](selector)
  303. .boundingClientRect(rect => {
  304. if (all && Array.isArray(rect) && rect.length) {
  305. resolve(rect)
  306. }
  307. if (!all && rect) {
  308. resolve(rect)
  309. }
  310. })
  311. .exec()
  312. })
  313. },
  314. }
  315. };
  316. </script>
  317. <style lang="scss" scoped>
  318. .cj-slider {
  319. position: relative;
  320. // background-color: #ebedf0;
  321. &__line {
  322. position: absolute;
  323. width: 100%;
  324. background-color: #ebedf0;
  325. }
  326. &__gap {
  327. position: relative;
  328. // 动画有bug,暂时取消
  329. // transition: width 0.2s;
  330. background-color: #1989fa;
  331. }
  332. &__button {
  333. width: 24px;
  334. height: 24px;
  335. border-radius: 50%;
  336. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  337. background-color: #fff;
  338. cursor: pointer;
  339. }
  340. &__button-wrap {
  341. position: absolute;
  342. top: 50%;
  343. left: 0;
  344. transform: translate3d(-50%, -50%, 0);
  345. }
  346. &__button-wrap2 {
  347. position: absolute;
  348. top: 50%;
  349. right: 0;
  350. transform: translate3d(50%, -50%, 0);
  351. }
  352. }
  353. .cj-slider--disabled {
  354. opacity: 0.5;
  355. }
  356. .mouse-move {
  357. position: fixed;
  358. left: 0;
  359. right: 0;
  360. transform: translateY(-50%);
  361. opacity: 0;
  362. }
  363. </style>