jxs-slider.vue 14 KB

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