Files
WAI_Project_UNIX/uni_modules/cool-ui/components/cl-slider/cl-slider.uvue

551 lines
14 KiB
Plaintext
Raw Normal View History

2025-07-21 16:47:04 +08:00
<template>
<view
class="cl-slider"
:class="[
{
2025-08-06 16:30:49 +08:00
'cl-slider--disabled': isDisabled
2025-07-21 16:47:04 +08:00
},
pt.className
]"
>
<view
class="cl-slider__inner"
:style="{
2025-08-07 18:52:42 +08:00
height: blockSize + 8 + 'rpx'
2025-07-21 16:47:04 +08:00
}"
>
2025-07-29 15:51:03 +08:00
<view
class="cl-slider__track"
:class="[pt.track?.className]"
:style="{
height: trackHeight + 'rpx'
}"
>
2025-07-29 22:47:12 +08:00
<view
class="cl-slider__progress"
:class="[pt.progress?.className]"
:style="progressStyle"
></view>
2025-09-03 19:03:39 +08:00
</view>
2025-07-29 19:18:58 +08:00
2025-09-03 19:03:39 +08:00
<!-- 单滑块模式 -->
<view
v-if="!range"
class="cl-slider__thumb"
:class="[pt.thumb?.className]"
:style="singleThumbStyle"
></view>
<!-- 双滑块模式 -->
<template v-if="range">
2025-07-21 16:47:04 +08:00
<view
2025-09-03 19:03:39 +08:00
class="cl-slider__thumb cl-slider__thumb--min"
2025-07-29 19:18:58 +08:00
:class="[pt.thumb?.className]"
2025-09-03 19:03:39 +08:00
:style="minThumbStyle"
2025-07-21 16:47:04 +08:00
></view>
2025-09-03 19:03:39 +08:00
<view
class="cl-slider__thumb cl-slider__thumb--max"
:class="[pt.thumb?.className]"
:style="maxThumbStyle"
></view>
</template>
2025-07-21 16:47:04 +08:00
<view
class="cl-slider__picker"
:style="{
2025-07-29 15:51:03 +08:00
height: blockSize * 1.5 + 'rpx'
2025-07-21 16:47:04 +08:00
}"
2025-07-29 19:18:58 +08:00
@touchstart.prevent="onTouchStart"
@touchmove.prevent="onTouchMove"
2025-07-21 16:47:04 +08:00
@touchend="onTouchEnd"
@touchcancel="onTouchEnd"
></view>
</view>
2025-08-06 16:30:49 +08:00
<slot name="value" :value="displayValue">
<cl-text
v-if="showValue"
:pt="{
className: parseClass(['text-center w-[100rpx]', pt.value?.className])
}"
>
{{ displayValue }}
</cl-text>
</slot>
2025-07-21 16:47:04 +08:00
</view>
</template>
<script setup lang="ts">
2025-07-29 19:18:58 +08:00
import { computed, getCurrentInstance, nextTick, onMounted, ref, watch, type PropType } from "vue";
import { parseClass, parsePt, rpx2px } from "@/cool";
2025-07-21 16:47:04 +08:00
import type { PassThroughProps } from "../../types";
2025-08-06 16:30:49 +08:00
import { useForm } from "../../hooks";
2025-07-21 16:47:04 +08:00
defineOptions({
name: "cl-slider"
});
// 组件属性定义
const props = defineProps({
// 样式穿透对象
pt: {
type: Object,
default: () => ({})
},
2025-07-29 19:18:58 +08:00
// v-model 绑定的值,单值模式使用
2025-07-21 16:47:04 +08:00
modelValue: {
type: Number,
default: 0
},
2025-07-29 19:18:58 +08:00
// v-model:values 绑定的值,范围模式使用
values: {
type: Array as PropType<number[]>,
default: () => [0, 0]
},
2025-07-21 16:47:04 +08:00
// 最小值
min: {
type: Number,
default: 0
},
// 最大值
max: {
type: Number,
default: 100
},
// 步长
step: {
type: Number,
default: 1
},
// 是否禁用
disabled: {
type: Boolean,
default: false
},
2025-07-29 15:51:03 +08:00
// 滑块的大小
2025-07-21 16:47:04 +08:00
blockSize: {
type: Number,
2025-07-29 15:51:03 +08:00
default: 40
},
// 线的高度
trackHeight: {
type: Number,
default: 8
2025-07-21 16:47:04 +08:00
},
// 是否显示当前值
showValue: {
type: Boolean,
default: false
2025-07-29 19:18:58 +08:00
},
// 是否启用范围选择
range: {
type: Boolean,
default: false
2025-07-21 16:47:04 +08:00
}
});
2025-07-29 19:18:58 +08:00
const emit = defineEmits(["update:modelValue", "update:values", "change", "changing"]);
2025-07-21 16:47:04 +08:00
const { proxy } = getCurrentInstance()!;
// 样式穿透类型定义
type PassThrough = {
className?: string;
track?: PassThroughProps;
progress?: PassThroughProps;
thumb?: PassThroughProps;
value?: PassThroughProps;
};
// 计算样式穿透对象
const pt = computed(() => parsePt<PassThrough>(props.pt));
2025-08-06 16:30:49 +08:00
// cl-form 上下文
const { disabled } = useForm();
// 是否禁用
const isDisabled = computed(() => props.disabled || disabled.value);
2025-07-29 19:18:58 +08:00
// 当前滑块的值,单值模式
2025-07-21 16:47:04 +08:00
const value = ref<number>(props.modelValue);
2025-07-29 19:18:58 +08:00
// 当前范围值,范围模式
const rangeValue = ref<number[]>([...props.values]);
2025-07-21 16:47:04 +08:00
// 轨道宽度(像素)
const trackWidth = ref<number>(0);
// 轨道左侧距离屏幕的距离(像素)
const trackLeft = ref<number>(0);
2025-07-29 19:18:58 +08:00
// 当前活动的滑块索引0: min, 1: max仅在范围模式下使用
const activeThumbIndex = ref<number>(0);
2025-07-29 22:47:12 +08:00
// 计算当前值在滑块轨道上的百分比位置(单值模式专用)
2025-07-21 16:47:04 +08:00
const percentage = computed(() => {
2025-07-29 19:18:58 +08:00
if (props.range) return 0;
2025-07-21 16:47:04 +08:00
return ((value.value - props.min) / (props.max - props.min)) * 100;
});
2025-07-29 22:47:12 +08:00
// 计算范围模式下两个滑块的百分比位置
2025-07-29 19:18:58 +08:00
type RangePercentage = {
2025-07-29 22:47:12 +08:00
min: number; // 最小值滑块的位置百分比
max: number; // 最大值滑块的位置百分比
2025-07-29 19:18:58 +08:00
};
2025-07-29 22:47:12 +08:00
2025-07-29 19:18:58 +08:00
const rangePercentage = computed<RangePercentage>(() => {
if (!props.range) return { min: 0, max: 0 };
2025-07-29 22:47:12 +08:00
const currentValues = rangeValue.value;
const valueRange = props.max - props.min;
// 分别计算两个滑块在轨道上的位置百分比
const minPercent = ((currentValues[0] - props.min) / valueRange) * 100;
const maxPercent = ((currentValues[1] - props.min) / valueRange) * 100;
2025-07-29 19:18:58 +08:00
return { min: minPercent, max: maxPercent };
});
2025-07-29 22:47:12 +08:00
// 计算进度条的样式属性
2025-07-29 19:18:58 +08:00
const progressStyle = computed(() => {
2025-07-29 22:30:33 +08:00
const style = {};
2025-07-21 16:47:04 +08:00
2025-07-29 19:18:58 +08:00
if (props.range) {
2025-07-29 22:47:12 +08:00
// 范围模式:进度条从最小值滑块延伸到最大值滑块
2025-07-29 19:18:58 +08:00
const { min, max } = rangePercentage.value;
2025-07-29 22:30:33 +08:00
style["left"] = `${min}%`;
style["width"] = `${max - min}%`;
2025-07-29 19:18:58 +08:00
} else {
2025-07-29 22:47:12 +08:00
// 单值模式:进度条从轨道起点延伸到当前滑块位置
2025-07-29 22:30:33 +08:00
style["width"] = `${percentage.value}%`;
2025-07-21 16:47:04 +08:00
}
2025-07-29 19:18:58 +08:00
return style;
});
2025-07-21 16:47:04 +08:00
2025-07-29 22:47:12 +08:00
// 创建滑块的定位样式(通用函数)
function createThumbStyle(percentPosition: number) {
2025-07-29 22:30:33 +08:00
const style = {};
2025-07-29 19:18:58 +08:00
2025-07-29 22:47:12 +08:00
// 计算滑块的有效移动范围(扣除滑块自身宽度,防止超出轨道)
2025-07-29 19:18:58 +08:00
const effectiveTrackWidth = trackWidth.value - rpx2px(props.blockSize) + 1;
2025-07-29 22:47:12 +08:00
const leftPosition = (percentPosition / 100) * effectiveTrackWidth;
2025-07-21 16:47:04 +08:00
2025-07-29 22:47:12 +08:00
// 确保滑块位置在有效范围内
const finalLeftPosition = Math.max(0, Math.min(effectiveTrackWidth, leftPosition));
// 设置滑块的位置和尺寸
style["left"] = `${finalLeftPosition}px`;
2025-07-29 22:30:33 +08:00
style["width"] = `${rpx2px(props.blockSize)}px`;
style["height"] = `${rpx2px(props.blockSize)}px`;
2025-07-21 16:47:04 +08:00
return style;
2025-07-29 19:18:58 +08:00
}
2025-07-29 22:47:12 +08:00
// 单值模式滑块的样式
2025-07-29 19:18:58 +08:00
const singleThumbStyle = computed(() => {
return createThumbStyle(percentage.value);
});
2025-07-29 22:47:12 +08:00
// 范围模式最小值滑块的样式
2025-07-29 19:18:58 +08:00
const minThumbStyle = computed(() => {
return createThumbStyle(rangePercentage.value.min);
});
2025-07-29 22:47:12 +08:00
// 范围模式最大值滑块的样式
2025-07-29 19:18:58 +08:00
const maxThumbStyle = computed(() => {
return createThumbStyle(rangePercentage.value.max);
});
2025-07-29 22:47:12 +08:00
// 计算要显示的数值文本
2025-07-29 19:18:58 +08:00
const displayValue = computed<string>(() => {
if (props.range) {
2025-07-29 22:47:12 +08:00
// 范围模式:显示"最小值 - 最大值"格式
const currentValues = rangeValue.value;
return `${currentValues[0]} - ${currentValues[1]}`;
2025-07-29 19:18:58 +08:00
}
2025-07-29 22:47:12 +08:00
// 单值模式:显示当前值
2025-07-29 19:18:58 +08:00
return `${value.value}`;
2025-07-21 16:47:04 +08:00
});
2025-07-29 22:47:12 +08:00
// 获取滑块轨道的位置和尺寸信息,这是触摸计算的基础数据
2025-07-29 22:30:33 +08:00
function getTrackInfo(): Promise<void> {
return new Promise((resolve) => {
uni.createSelectorQuery()
.in(proxy)
.select(".cl-slider__track")
.boundingClientRect((node) => {
2025-07-29 22:47:12 +08:00
// 保存轨道的宽度和左侧偏移量,用于后续的触摸位置计算
2025-07-29 22:30:33 +08:00
trackWidth.value = (node as NodeInfo).width ?? 0;
trackLeft.value = (node as NodeInfo).left ?? 0;
resolve();
})
.exec();
});
2025-07-21 16:47:04 +08:00
}
2025-07-29 22:47:12 +08:00
// 根据触摸点的横坐标计算对应的滑块数值
2025-07-21 16:47:04 +08:00
function calculateValue(clientX: number): number {
2025-07-29 22:47:12 +08:00
// 如果轨道宽度为0还未初始化直接返回最小值
2025-07-29 19:18:58 +08:00
if (trackWidth.value == 0) return props.min;
2025-07-21 16:47:04 +08:00
2025-07-29 22:47:12 +08:00
// 计算触摸点相对于轨道左侧的偏移距离
const touchOffset = clientX - trackLeft.value;
// 将偏移距离转换为0~1的百分比并限制在有效范围内
const progressPercentage = Math.max(0, Math.min(1, touchOffset / trackWidth.value));
// 根据百分比计算在min~max范围内的实际数值
const valueRange = props.max - props.min;
let calculatedValue = props.min + progressPercentage * valueRange;
2025-07-21 16:47:04 +08:00
2025-07-29 22:47:12 +08:00
// 如果设置了步长,按步长进行取整对齐
2025-07-21 16:47:04 +08:00
if (props.step > 0) {
2025-07-29 22:47:12 +08:00
calculatedValue =
Math.round((calculatedValue - props.min) / props.step) * props.step + props.min;
2025-07-21 16:47:04 +08:00
}
2025-07-29 22:47:12 +08:00
// 确保最终值在[min, max]范围内
return Math.max(props.min, Math.min(props.max, calculatedValue));
2025-07-21 16:47:04 +08:00
}
2025-07-29 22:47:12 +08:00
// 在范围模式下,根据触摸点离哪个滑块更近来确定应该移动哪个滑块
function determineActiveThumb(clientX: number): number {
2025-07-29 19:18:58 +08:00
if (!props.range) return 0;
2025-07-29 22:47:12 +08:00
const currentValues = rangeValue.value;
const touchValue = calculateValue(clientX);
2025-07-29 19:18:58 +08:00
2025-07-29 22:47:12 +08:00
// 计算触摸位置到两个滑块的距离,选择距离更近的滑块进行操作
const distanceToMinThumb = Math.abs(touchValue - currentValues[0]);
const distanceToMaxThumb = Math.abs(touchValue - currentValues[1]);
2025-07-29 19:18:58 +08:00
2025-07-29 22:47:12 +08:00
// 返回距离更近的滑块索引0: 最小值滑块1: 最大值滑块)
return distanceToMinThumb <= distanceToMaxThumb ? 0 : 1;
2025-07-29 19:18:58 +08:00
}
// 更新滑块的值,并触发相应的事件
2025-07-29 22:47:12 +08:00
function updateValue(newValue: number | number[]) {
2025-07-29 19:18:58 +08:00
if (props.range) {
2025-07-29 22:47:12 +08:00
// 范围模式:处理双滑块
const newRangeValues = newValue as number[];
const currentRangeValues = rangeValue.value;
2025-07-29 19:18:58 +08:00
2025-07-29 22:47:12 +08:00
// 当左滑块超过右滑块时,自动交换活动滑块索引,实现滑块角色互换
if (newRangeValues[0] > newRangeValues[1]) {
activeThumbIndex.value = 1 - activeThumbIndex.value; // 0变11变0
2025-07-29 19:18:58 +08:00
}
2025-07-29 22:47:12 +08:00
// 确保最小值始终不大于最大值,自动排序
const sortedValues = [
Math.min(newRangeValues[0], newRangeValues[1]),
Math.max(newRangeValues[0], newRangeValues[1])
];
// 只有值真正改变时才更新和触发事件,避免不必要的渲染
if (JSON.stringify(currentRangeValues) !== JSON.stringify(sortedValues)) {
rangeValue.value = sortedValues;
emit("update:values", sortedValues);
emit("changing", sortedValues);
2025-07-29 19:18:58 +08:00
}
} else {
2025-07-29 22:47:12 +08:00
// 单值模式:处理单个滑块
const newSingleValue = newValue as number;
const currentSingleValue = value.value;
if (currentSingleValue !== newSingleValue) {
value.value = newSingleValue;
emit("update:modelValue", newSingleValue);
emit("changing", newSingleValue);
2025-07-29 19:18:58 +08:00
}
2025-07-21 16:47:04 +08:00
}
}
2025-07-29 22:47:12 +08:00
// 触摸开始事件:获取轨道信息并初始化滑块位置
2025-07-29 22:30:33 +08:00
async function onTouchStart(e: TouchEvent) {
2025-08-06 16:30:49 +08:00
if (isDisabled.value) return;
2025-07-21 16:47:04 +08:00
2025-07-29 22:47:12 +08:00
// 先获取轨道的位置和尺寸信息,这是后续计算的基础
2025-07-29 22:30:33 +08:00
await getTrackInfo();
2025-07-21 16:47:04 +08:00
2025-07-29 22:47:12 +08:00
// 等待DOM更新后再处理触摸逻辑
2025-07-29 22:30:33 +08:00
nextTick(() => {
2025-07-21 16:47:04 +08:00
const clientX = e.touches[0].clientX;
2025-07-29 22:47:12 +08:00
const calculatedValue = calculateValue(clientX);
2025-07-29 19:18:58 +08:00
if (props.range) {
2025-07-29 22:47:12 +08:00
// 范围模式:确定要操作的滑块,然后更新对应滑块的值
2025-07-29 19:18:58 +08:00
activeThumbIndex.value = determineActiveThumb(clientX);
2025-07-29 22:47:12 +08:00
const updatedValues = [...rangeValue.value];
updatedValues[activeThumbIndex.value] = calculatedValue;
updateValue(updatedValues);
2025-07-29 19:18:58 +08:00
} else {
2025-07-29 22:47:12 +08:00
// 单值模式:直接更新滑块值
updateValue(calculatedValue);
2025-07-29 19:18:58 +08:00
}
2025-07-29 22:30:33 +08:00
});
2025-07-21 16:47:04 +08:00
}
2025-07-29 22:47:12 +08:00
// 触摸移动事件:实时更新滑块位置
2025-07-29 19:18:58 +08:00
function onTouchMove(e: TouchEvent) {
2025-08-06 16:30:49 +08:00
if (isDisabled.value) return;
2025-07-21 16:47:04 +08:00
const clientX = e.touches[0].clientX;
2025-07-29 22:47:12 +08:00
const calculatedValue = calculateValue(clientX);
2025-07-29 19:18:58 +08:00
if (props.range) {
2025-07-29 22:47:12 +08:00
// 范围模式:更新当前活动滑块的值
const updatedValues = [...rangeValue.value];
updatedValues[activeThumbIndex.value] = calculatedValue;
updateValue(updatedValues);
2025-07-29 19:18:58 +08:00
} else {
2025-07-29 22:47:12 +08:00
// 单值模式:直接更新滑块值
updateValue(calculatedValue);
2025-07-29 19:18:58 +08:00
}
2025-07-21 16:47:04 +08:00
}
2025-07-29 22:47:12 +08:00
// 触摸结束事件完成拖动触发最终的change事件
2025-07-21 16:47:04 +08:00
function onTouchEnd() {
2025-08-06 16:30:49 +08:00
if (isDisabled.value) return;
2025-07-29 22:47:12 +08:00
// 触发change事件表示用户完成了一次完整的拖动操作
2025-07-29 19:18:58 +08:00
if (props.range) {
emit("change", rangeValue.value);
} else {
emit("change", value.value);
}
2025-07-21 16:47:04 +08:00
}
2025-07-29 22:47:12 +08:00
// 监听外部传入的modelValue变化保持单值模式内部状态同步
2025-07-21 16:47:04 +08:00
watch(
computed(() => props.modelValue),
2025-07-29 22:47:12 +08:00
(newModelValue: number) => {
// 当外部值与内部值不同时,更新内部值并限制在有效范围内
if (newModelValue !== value.value) {
value.value = Math.max(props.min, Math.min(props.max, newModelValue));
2025-07-21 16:47:04 +08:00
}
},
{ immediate: true }
);
2025-07-29 22:47:12 +08:00
// 监听外部传入的values变化保持范围模式内部状态同步
2025-07-29 19:18:58 +08:00
watch(
computed(() => props.values),
2025-07-29 22:47:12 +08:00
(newValues: number[]) => {
// 将外部传入的数组值映射并限制在有效范围内
rangeValue.value = newValues.map((singleValue) => {
return Math.max(props.min, Math.min(props.max, singleValue));
2025-07-29 19:18:58 +08:00
});
},
{ immediate: true }
);
2025-07-29 22:47:12 +08:00
// 监听最大值变化,确保当前值不会超过新的最大值
2025-07-21 16:47:04 +08:00
watch(
computed(() => props.max),
2025-07-29 22:47:12 +08:00
(newMaxValue: number) => {
2025-07-29 19:18:58 +08:00
if (props.range) {
2025-07-29 22:47:12 +08:00
// 范围模式:检查并调整两个滑块的值
const currentRangeValues = rangeValue.value;
if (currentRangeValues[0] > newMaxValue || currentRangeValues[1] > newMaxValue) {
updateValue([
Math.min(currentRangeValues[0], newMaxValue),
Math.min(currentRangeValues[1], newMaxValue)
]);
2025-07-29 19:18:58 +08:00
}
} else {
2025-07-29 22:47:12 +08:00
// 单值模式:检查并调整单个滑块的值
const currentSingleValue = value.value;
if (currentSingleValue > newMaxValue) {
updateValue(newMaxValue);
2025-07-29 19:18:58 +08:00
}
2025-07-21 16:47:04 +08:00
}
},
2025-07-29 19:18:58 +08:00
{ immediate: true }
2025-07-21 16:47:04 +08:00
);
2025-07-29 22:47:12 +08:00
// 监听最小值变化,确保当前值不会小于新的最小值
2025-07-21 16:47:04 +08:00
watch(
computed(() => props.min),
2025-07-29 22:47:12 +08:00
(newMinValue: number) => {
2025-07-29 19:18:58 +08:00
if (props.range) {
2025-07-29 22:47:12 +08:00
// 范围模式:检查并调整两个滑块的值
const currentRangeValues = rangeValue.value;
if (currentRangeValues[0] < newMinValue || currentRangeValues[1] < newMinValue) {
updateValue([
Math.max(currentRangeValues[0], newMinValue),
Math.max(currentRangeValues[1], newMinValue)
]);
2025-07-29 19:18:58 +08:00
}
} else {
2025-07-29 22:47:12 +08:00
// 单值模式:检查并调整单个滑块的值
const currentSingleValue = value.value;
if (currentSingleValue < newMinValue) {
updateValue(newMinValue);
2025-07-29 19:18:58 +08:00
}
2025-07-21 16:47:04 +08:00
}
},
2025-07-29 19:18:58 +08:00
{ immediate: true }
2025-07-21 16:47:04 +08:00
);
onMounted(() => {
2025-07-29 19:18:58 +08:00
watch(
computed(() => [props.showValue]),
() => {
nextTick(() => {
getTrackInfo();
});
}
);
2025-07-21 16:47:04 +08:00
getTrackInfo();
});
</script>
<style lang="scss" scoped>
.cl-slider {
2025-07-29 15:51:03 +08:00
@apply flex flex-row items-center w-full overflow-visible;
2025-07-21 16:47:04 +08:00
&--disabled {
2025-08-06 16:30:49 +08:00
@apply opacity-50;
2025-07-21 16:47:04 +08:00
}
&__inner {
2025-07-29 15:51:03 +08:00
@apply flex-1 relative h-full flex flex-row items-center overflow-visible;
2025-07-21 16:47:04 +08:00
}
&__picker {
@apply absolute left-0 w-full;
}
&__track {
2025-07-29 15:51:03 +08:00
@apply relative w-full rounded-full overflow-visible;
2025-07-21 16:47:04 +08:00
@apply bg-surface-200;
}
&__progress {
2025-07-29 19:18:58 +08:00
@apply absolute top-0 h-full rounded-full;
2025-07-21 16:47:04 +08:00
@apply bg-primary-400;
}
&__thumb {
2025-09-03 19:03:39 +08:00
@apply absolute rounded-full border border-solid border-white;
2025-07-21 16:47:04 +08:00
@apply bg-primary-500;
pointer-events: none;
2025-07-29 19:18:58 +08:00
z-index: 1;
2025-08-06 16:30:49 +08:00
border-width: 4rpx;
box-shadow: 0 0 2rpx 2rpx rgba(100, 100, 100, 0.1);
2025-07-29 19:18:58 +08:00
&--min {
z-index: 2;
}
&--max {
z-index: 2;
}
2025-07-21 16:47:04 +08:00
}
}
</style>