支持范围选择

This commit is contained in:
icssoa
2025-07-29 22:30:33 +08:00
parent c0b5305daa
commit 27bb7e36b1

View File

@@ -189,14 +189,14 @@ const rangePercentage = computed<RangePercentage>(() => {
// 进度条样式 // 进度条样式
const progressStyle = computed(() => { const progressStyle = computed(() => {
const style = new Map<string, string>(); const style = {};
if (props.range) { if (props.range) {
const { min, max } = rangePercentage.value; const { min, max } = rangePercentage.value;
style.set("left", `${min}%`); style["left"] = `${min}%`;
style.set("width", `${max - min}%`); style["width"] = `${max - min}%`;
} else { } else {
style.set("width", `${percentage.value}%`); style["width"] = `${percentage.value}%`;
} }
return style; return style;
@@ -204,16 +204,16 @@ const progressStyle = computed(() => {
// 创建滑块样式的通用函数 // 创建滑块样式的通用函数
function createThumbStyle(percent: number) { function createThumbStyle(percent: number) {
const style = new Map<string, string>(); const style = {};
// 使用像素定位,确保滑块始终在轨道范围内 // 使用像素定位,确保滑块始终在轨道范围内
const effectiveTrackWidth = trackWidth.value - rpx2px(props.blockSize) + 1; const effectiveTrackWidth = trackWidth.value - rpx2px(props.blockSize) + 1;
const leftPosition = (percent / 100) * effectiveTrackWidth; const leftPosition = (percent / 100) * effectiveTrackWidth;
const finalLeft = Math.max(0, Math.min(effectiveTrackWidth, leftPosition)); const finalLeft = Math.max(0, Math.min(effectiveTrackWidth, leftPosition));
style.set("left", `${finalLeft}px`); style["left"] = `${finalLeft}px`;
style.set("width", `${rpx2px(props.blockSize)}px`); style["width"] = `${rpx2px(props.blockSize)}px`;
style.set("height", `${rpx2px(props.blockSize)}px`); style["height"] = `${rpx2px(props.blockSize)}px`;
return style; return style;
} }
@@ -244,15 +244,19 @@ const displayValue = computed<string>(() => {
}); });
// 获取滑块轨道的宽度和左边距,用于后续触摸计算 // 获取滑块轨道的宽度和左边距,用于后续触摸计算
function getTrackInfo() { function getTrackInfo(): Promise<void> {
uni.createSelectorQuery() return new Promise((resolve) => {
.in(proxy) uni.createSelectorQuery()
.select(".cl-slider__track") .in(proxy)
.boundingClientRect((node) => { .select(".cl-slider__track")
trackWidth.value = (node as NodeInfo).width ?? 0; .boundingClientRect((node) => {
trackLeft.value = (node as NodeInfo).left ?? 0; trackWidth.value = (node as NodeInfo).width ?? 0;
}) trackLeft.value = (node as NodeInfo).left ?? 0;
.exec();
resolve();
})
.exec();
});
} }
// 根据触摸点的clientX计算对应的滑块值 // 根据触摸点的clientX计算对应的滑块值
@@ -326,13 +330,12 @@ function updateValue(val: number | number[]) {
} }
// 触摸开始事件,获取轨道信息并初步设置值 // 触摸开始事件,获取轨道信息并初步设置值
function onTouchStart(e: TouchEvent) { async function onTouchStart(e: TouchEvent) {
if (props.disabled) return; if (props.disabled) return;
getTrackInfo(); await getTrackInfo();
// 延迟10ms确保轨道信息已获取 nextTick(() => {
setTimeout(() => {
const clientX = e.touches[0].clientX; const clientX = e.touches[0].clientX;
const newValue = calculateValue(clientX); const newValue = calculateValue(clientX);
@@ -344,7 +347,7 @@ function onTouchStart(e: TouchEvent) {
} else { } else {
updateValue(newValue); updateValue(newValue);
} }
}, 10); });
} }
// 触摸移动事件,实时更新滑块值 // 触摸移动事件,实时更新滑块值