@@ -29,6 +29,10 @@ export default {
2929 type: Number ,
3030 required: true ,
3131 },
32+ step: {
33+ type: Number ,
34+ default: 1 ,
35+ },
3236 disabled: {
3337 type: Boolean ,
3438 default: false ,
@@ -39,33 +43,51 @@ export default {
3943 const sliderEvent = {
4044 isMsDown: false ,
4145 msDownX: 0 ,
42- msDownValue : 0 ,
46+ msDownRatio : 0 ,
4347 }
4448 const dom_sliderBar = ref (null )
4549
50+ const clampValue = val => {
51+ if (val < props .min ) return props .min
52+ if (val > props .max ) return props .max
53+ return val
54+ }
55+ const getSteppedValue = val => {
56+ const step = props .step > 0 ? props .step : 1
57+ const stepped = Math .round ((val - props .min ) / step) * step + props .min
58+ return clampValue (Number (stepped .toFixed (10 )))
59+ }
60+ const getSliderWidth = () => dom_sliderBar .value ? .clientWidth || 0
61+ const getRange = () => props .max - props .min
62+ const emitSteppedValue = rawValue => {
63+ const value = getSteppedValue (rawValue)
64+ emit (' change' , value)
65+ return value
66+ }
67+
4668 const handleSliderMsDown = event => {
4769 if (props .disabled ) return
70+ const width = getSliderWidth ()
71+ if (! width) return
4872
4973 sliderEvent .isMsDown = true
5074 sliderEvent .msDownX = event .clientX
5175
52- sliderEvent .msDownValue = event .offsetX / dom_sliderBar .value .clientWidth
53- let val = sliderEvent .msDownValue * (props .max - props .min ) + props .min
54- if (val < props .min ) val = props .min
55- if (val > props .max ) val = props .max
56- emit (' change' , val)
57-
58- // if (isMute.value) window.app_event.setSliderIsMute(false)
76+ const rawValue = (event .offsetX / width) * getRange () + props .min
77+ const value = emitSteppedValue (rawValue)
78+ sliderEvent .msDownRatio = getRange () === 0 ? 0 : (value - props .min ) / getRange ()
5979 }
6080 const handleSliderMsUp = () => {
6181 sliderEvent .isMsDown = false
6282 }
6383 const handleSliderMsMove = event => {
6484 if (! sliderEvent .isMsDown || props .disabled ) return
65- let value = (sliderEvent .msDownValue + (event .clientX - sliderEvent .msDownX ) / dom_sliderBar .value .clientWidth ) * (props .max - props .min ) + props .min
66- if (value > props .max ) value = props .max
67- else if (value < props .min ) value = props .min
68- emit (' change' , value)
85+ const width = getSliderWidth ()
86+ if (! width) return
87+
88+ const ratio = sliderEvent .msDownRatio + (event .clientX - sliderEvent .msDownX ) / width
89+ const rawValue = ratio * getRange () + props .min
90+ emitSteppedValue (rawValue)
6991 }
7092
7193 document .addEventListener (' mousemove' , handleSliderMsMove)
0 commit comments