index.vue 14 KB

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