@@ -59,7 +59,7 @@ import { computed, getCurrentInstance, onMounted, ref } from 'vue'
5959import { getRect , isArray , isDef , isNumber , uuid } from ' ../common/util'
6060import { useTouch } from ' ../composables/useTouch'
6161import { watch } from ' vue'
62- import { sliderProps } from ' ./types'
62+ import { sliderProps , type SliderExpose } from ' ./types'
6363
6464const props = defineProps (sliderProps )
6565const emit = defineEmits ([' dragstart' , ' dragmove' , ' dragend' , ' update:modelValue' ])
@@ -196,13 +196,20 @@ const buttonRightStyle = computed(() => {
196196})
197197
198198onMounted (() => {
199+ initSlider ()
200+ })
201+
202+ /**
203+ * 初始化slider宽度
204+ */
205+ function initSlider() {
199206 getRect (` #${sliderId .value } ` , false , proxy ).then ((data ) => {
200207 // trackWidth: 轨道全长
201208 trackWidth .value = Number (data .width )
202209 // trackLeft: 轨道距离左侧的距离
203210 trackLeft .value = Number (data .left )
204211 })
205- })
212+ }
206213
207214function onTouchStart(event : any ) {
208215 const { disabled, modelValue } = props
@@ -224,6 +231,7 @@ function onTouchMove(event: any) {
224231 // 移动间距 deltaX 就是向左(-)向右(+)
225232 const diff = (touchLeft .deltaX .value / trackWidth .value ) * (maxValue .value - minValue .value )
226233 newValue .value = startValue .value + diff
234+
227235 // 左滑轮滑动控制
228236 leftBarSlider (newValue .value )
229237 emit (' dragmove' , {
@@ -340,6 +348,10 @@ function calcBarPercent() {
340348 leftBarPercent .value = percent
341349 barStyle .value = ` width: ${percent }%; height: ${barHeight .value }; `
342350}
351+
352+ defineExpose <SliderExpose >({
353+ initSlider
354+ })
343355 </script >
344356<style lang="scss" scoped>
345357@import ' ./index.scss' ;
0 commit comments