diff --git a/components/Slider.vue b/components/Slider.vue index 521423c9..c560e9f8 100755 --- a/components/Slider.vue +++ b/components/Slider.vue @@ -35,6 +35,7 @@ const value = computed(() => restrict(model.value, 0)); const smoothValue = useSmoothValue(value, 0.5); // 修改这个参数可以调整滑动条的平滑移动值。 const buffered = computed(() => restrict(props.buffered, NaN)); + const thumbEl = ref(), trackEl = ref(); /** * 重置默认值。 @@ -54,13 +55,13 @@ */ function onThumbDown(e: PointerEvent, triggerByTrack: boolean = false) { if (e.button === 1) { resetDefault(e); return; } - const thumb = (e.target as HTMLDivElement).parentElement!.querySelector(".thumb") as HTMLDivElement; - const track = thumb.parentElement!.querySelector(".track")!; + const thumb = thumbEl.value!, track = trackEl.value!; + const thumbSize = thumb.offsetWidth; const { left, width: max } = track.getBoundingClientRect(); const x = triggerByTrack ? 0 : e.pageX - left - thumb.offsetLeft; const pointerMove = useDebounce((e: PointerEvent) => { const position = clamp(e.pageX - left - x, 0, max); - const value = map(position, 0, max, props.min, props.max); + const value = map(position, 0, max - thumbSize, props.min, props.max); model.value = value; emits("changing", value); }); @@ -79,9 +80,10 @@ */ async function onTrackDown(e: PointerEvent) { if (e.button === 1) { resetDefault(e); return; } - const track = e.target as HTMLDivElement; + const thumb = thumbEl.value!, track = trackEl.value!; + const thumbSizeHalf = thumb.offsetWidth / 2; const { width } = track.getBoundingClientRect(); - const value = map(e.offsetX, 0, width, props.min, props.max); + const value = map(e.offsetX, thumbSizeHalf, width - thumbSizeHalf, props.min, props.max); model.value = value; emits("changing", value); await nextTick(); @@ -112,10 +114,10 @@ aria-orientation="horizontal" > -
+
-
+
@@ -157,7 +159,7 @@ .buffered { @include oval; height: var(--track-thickness); - margin: $thumb-size-half; + margin: $thumb-size-half 0; } .track { @@ -175,7 +177,7 @@ } .passed { - width: $value; + width: calc($value + $thumb-size); background-color: c(accent); opacity: map(var(--value), 0, 1, 0.4, 1, true); } diff --git a/pages/components.vue b/pages/components.vue index 8b39498d..00d58e97 100755 --- a/pages/components.vue +++ b/pages/components.vue @@ -352,7 +352,7 @@
- +