This commit is contained in:
icssoa
2025-07-29 19:18:58 +08:00
parent db2cfc8e59
commit bb4caddc0b
4 changed files with 244 additions and 73 deletions

View File

@@ -1,4 +1,4 @@
import { base64ToBlob } from "./comm"; import { base64ToBlob, uuid } from "./comm";
export type CanvasToPngOptions = { export type CanvasToPngOptions = {
canvasId: string; canvasId: string;

View File

@@ -5,9 +5,19 @@
<cl-slider v-model="num"></cl-slider> <cl-slider v-model="num"></cl-slider>
</demo-item> </demo-item>
<demo-item :label="t('范围选择')">
<cl-text
:pt="{
className: 'mb-3'
}"
>{{ num2[0] }} {{ num2[1] }}</cl-text
>
<cl-slider v-model:values="num2" range></cl-slider>
</demo-item>
<demo-item :label="t('自定义')"> <demo-item :label="t('自定义')">
<cl-slider <cl-slider
v-model="num2" v-model="num3"
:disabled="isDisabled" :disabled="isDisabled"
:show-value="isShowValue" :show-value="isShowValue"
:block-size="isSize ? 50 : 40" :block-size="isSize ? 50 : 40"
@@ -53,7 +63,8 @@ import DemoItem from "../components/item.uvue";
import { t } from "@/locale"; import { t } from "@/locale";
const num = ref(60); const num = ref(60);
const num2 = ref(35); const num2 = ref<number[]>([10, 20]);
const num3 = ref(35);
const isDisabled = ref(false); const isDisabled = ref(false);
const isShowValue = ref(true); const isShowValue = ref(true);
const isStep = ref(false); const isStep = ref(false);

View File

@@ -21,15 +21,29 @@
height: trackHeight + 'rpx' height: trackHeight + 'rpx'
}" }"
> >
<view class="cl-slider__progress" :style="progressStyle"></view>
<!-- 单滑块模式 -->
<view <view
class="cl-slider__progress" v-if="!range"
:style="{ class="cl-slider__thumb"
width: percentage + '%' :class="[pt.thumb?.className]"
}" :style="singleThumbStyle"
></view> ></view>
<view class="cl-slider__thumb" :class="[pt.thumb?.className]" :style="thumbStyle"> <!-- 双滑块模式 -->
</view> <template v-if="range">
<view
class="cl-slider__thumb cl-slider__thumb--min"
:class="[pt.thumb?.className]"
:style="minThumbStyle"
></view>
<view
class="cl-slider__thumb cl-slider__thumb--max"
:class="[pt.thumb?.className]"
:style="maxThumbStyle"
></view>
</template>
</view> </view>
<view <view
@@ -37,8 +51,8 @@
:style="{ :style="{
height: blockSize * 1.5 + 'rpx' height: blockSize * 1.5 + 'rpx'
}" }"
@touchstart="onTouchStart" @touchstart.prevent="onTouchStart"
@touchmove="onTouchMove" @touchmove.prevent="onTouchMove"
@touchend="onTouchEnd" @touchend="onTouchEnd"
@touchcancel="onTouchEnd" @touchcancel="onTouchEnd"
></view> ></view>
@@ -50,14 +64,14 @@
className: parseClass(['text-center w-[100rpx]', pt.value?.className]) className: parseClass(['text-center w-[100rpx]', pt.value?.className])
}" }"
> >
{{ value }} {{ displayValue }}
</cl-text> </cl-text>
</view> </view>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, getCurrentInstance, nextTick, onMounted, ref, watch } from "vue"; import { computed, getCurrentInstance, nextTick, onMounted, ref, watch, type PropType } from "vue";
import { parseClass, parsePt, px2rpx, rpx2px } from "@/cool"; import { parseClass, parsePt, rpx2px } from "@/cool";
import type { PassThroughProps } from "../../types"; import type { PassThroughProps } from "../../types";
defineOptions({ defineOptions({
@@ -71,11 +85,16 @@ const props = defineProps({
type: Object, type: Object,
default: () => ({}) default: () => ({})
}, },
// v-model 绑定的值 // v-model 绑定的值,单值模式使用
modelValue: { modelValue: {
type: Number, type: Number,
default: 0 default: 0
}, },
// v-model:values 绑定的值,范围模式使用
values: {
type: Array as PropType<number[]>,
default: () => [0, 0]
},
// 最小值 // 最小值
min: { min: {
type: Number, type: Number,
@@ -110,10 +129,15 @@ const props = defineProps({
showValue: { showValue: {
type: Boolean, type: Boolean,
default: false default: false
},
// 是否启用范围选择
range: {
type: Boolean,
default: false
} }
}); });
const emit = defineEmits(["update:modelValue", "change", "changing"]); const emit = defineEmits(["update:modelValue", "update:values", "change", "changing"]);
const { proxy } = getCurrentInstance()!; const { proxy } = getCurrentInstance()!;
@@ -129,47 +153,94 @@ type PassThrough = {
// 计算样式穿透对象 // 计算样式穿透对象
const pt = computed(() => parsePt<PassThrough>(props.pt)); const pt = computed(() => parsePt<PassThrough>(props.pt));
// 当前滑块的值,受控于v-model // 当前滑块的值,单值模式
const value = ref<number>(props.modelValue); const value = ref<number>(props.modelValue);
// 当前范围值,范围模式
const rangeValue = ref<number[]>([...props.values]);
// 轨道宽度(像素) // 轨道宽度(像素)
const trackWidth = ref<number>(0); const trackWidth = ref<number>(0);
// 轨道左侧距离屏幕的距离(像素) // 轨道左侧距离屏幕的距离(像素)
const trackLeft = ref<number>(0); const trackLeft = ref<number>(0);
// 计算当前值在[min, max]区间内的百分比 // 当前活动的滑块索引0: min, 1: max),仅在范围模式下使用
const activeThumbIndex = ref<number>(0);
// 计算当前值的百分比(单值模式)
const percentage = computed(() => { const percentage = computed(() => {
// 公式:(当前值 - 最小值) / (最大值 - 最小值) * 100 if (props.range) return 0;
// 结果为0~100之间的百分比
return ((value.value - props.min) / (props.max - props.min)) * 100; return ((value.value - props.min) / (props.max - props.min)) * 100;
}); });
// 计算滑块thumb的样式 // 计算范围值的百分比(范围模式)
const thumbStyle = computed(() => { type RangePercentage = {
min: number;
max: number;
};
const rangePercentage = computed<RangePercentage>(() => {
if (!props.range) return { min: 0, max: 0 };
const val = rangeValue.value;
const minPercent = ((val[0] - props.min) / (props.max - props.min)) * 100;
const maxPercent = ((val[1] - props.min) / (props.max - props.min)) * 100;
return { min: minPercent, max: maxPercent };
});
// 进度条样式
const progressStyle = computed(() => {
const style = new Map<string, string>(); const style = new Map<string, string>();
// 如果轨道宽度还没获取到,先用百分比定位 if (props.range) {
if (trackWidth.value == 0) { const { min, max } = rangePercentage.value;
style.set("left", `${percentage.value}%`); style.set("left", `${min}%`);
style.set("transform", `translateX(-50%)`); style.set("width", `${max - min}%`);
} else {
style.set("width", `${percentage.value}%`);
} }
// 使用像素定位,确保滑块始终在轨道范围内 return style;
// 可滑动的有效区域 = 轨道宽度 - 滑块宽度 });
const effectiveTrackWidth = trackWidth.value - rpx2px(props.blockSize);
// 滑块左边距 = 百分比 * 有效轨道宽度
const leftPosition = (percentage.value / 100) * effectiveTrackWidth;
// 限制在有效范围内 // 创建滑块样式的通用函数
function createThumbStyle(percent: number) {
const style = new Map<string, string>();
// 使用像素定位,确保滑块始终在轨道范围内
const effectiveTrackWidth = trackWidth.value - rpx2px(props.blockSize) + 1;
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.set("left", `${finalLeft}px`);
style.set("width", `${props.blockSize}rpx`); style.set("width", `${rpx2px(props.blockSize)}px`);
style.set("height", `${props.blockSize}rpx`); style.set("height", `${rpx2px(props.blockSize)}px`);
return style; return style;
}
// 单滑块样式
const singleThumbStyle = computed(() => {
return createThumbStyle(percentage.value);
});
// 最小值滑块样式
const minThumbStyle = computed(() => {
return createThumbStyle(rangePercentage.value.min);
});
// 最大值滑块样式
const maxThumbStyle = computed(() => {
return createThumbStyle(rangePercentage.value.max);
});
// 显示的值
const displayValue = computed<string>(() => {
if (props.range) {
const val = rangeValue.value;
return `${val[0]} - ${val[1]}`;
}
return `${value.value}`;
}); });
// 获取滑块轨道的宽度和左边距,用于后续触摸计算 // 获取滑块轨道的宽度和左边距,用于后续触摸计算
@@ -186,8 +257,8 @@ function getTrackInfo() {
// 根据触摸点的clientX计算对应的滑块值 // 根据触摸点的clientX计算对应的滑块值
function calculateValue(clientX: number): number { function calculateValue(clientX: number): number {
// 如果轨道宽度为0直接返回当前 // 如果轨道宽度为0直接返回最小
if (trackWidth.value == 0) return value.value; if (trackWidth.value == 0) return props.min;
// 计算触摸点距离轨道左侧的偏移 // 计算触摸点距离轨道左侧的偏移
const offset = clientX - trackLeft.value; const offset = clientX - trackLeft.value;
@@ -207,17 +278,55 @@ function calculateValue(clientX: number): number {
return Math.max(props.min, Math.min(props.max, val)); return Math.max(props.min, Math.min(props.max, val));
} }
// 更新滑块的值并触发v-model和changing事件 // 在范围模式下,确定应该移动哪个滑块
function updateValue(val: number) { function determineActiveThumb(clientX: number) {
if (val !== value.value) { if (!props.range) return 0;
value.value = val;
emit("update:modelValue", val); const val = rangeValue.value;
emit("changing", val); const clickValue = calculateValue(clientX);
// 计算点击位置到两个滑块的距离
const distToMin = Math.abs(clickValue - val[0]);
const distToMax = Math.abs(clickValue - val[1]);
// 选择距离更近的滑块
return distToMin <= distToMax ? 0 : 1;
}
// 更新滑块的值,并触发相应的事件
function updateValue(val: number | number[]) {
if (props.range) {
const newVal = val as number[];
const oldVal = rangeValue.value;
// 如果最小值超过了最大值交换activeThumbIndex
if (newVal[0] > newVal[1]) {
activeThumbIndex.value = activeThumbIndex.value == 0 ? 1 : 0;
}
// 确保最小值不大于最大值,但允许通过拖动交换角色
const sortedVal = [Math.min(newVal[0], newVal[1]), Math.max(newVal[0], newVal[1])];
// 检查值是否真的改变了
if (JSON.stringify(oldVal) !== JSON.stringify(sortedVal)) {
rangeValue.value = sortedVal;
emit("update:values", sortedVal);
emit("changing", sortedVal);
}
} else {
const newVal = val as number;
const oldVal = value.value;
if (oldVal !== newVal) {
value.value = newVal;
emit("update:modelValue", newVal);
emit("changing", newVal);
}
} }
} }
// 触摸开始事件,获取轨道信息并初步设置值 // 触摸开始事件,获取轨道信息并初步设置值
function onTouchStart(e: UniTouchEvent) { function onTouchStart(e: TouchEvent) {
if (props.disabled) return; if (props.disabled) return;
getTrackInfo(); getTrackInfo();
@@ -225,75 +334,115 @@ function onTouchStart(e: UniTouchEvent) {
// 延迟10ms确保轨道信息已获取 // 延迟10ms确保轨道信息已获取
setTimeout(() => { setTimeout(() => {
const clientX = e.touches[0].clientX; const clientX = e.touches[0].clientX;
const value = calculateValue(clientX); const newValue = calculateValue(clientX);
updateValue(value);
if (props.range) {
activeThumbIndex.value = determineActiveThumb(clientX);
const val = [...rangeValue.value];
val[activeThumbIndex.value] = newValue;
updateValue(val);
} else {
updateValue(newValue);
}
}, 10); }, 10);
} }
// 触摸移动事件,实时更新滑块值 // 触摸移动事件,实时更新滑块值
function onTouchMove(e: UniTouchEvent) { function onTouchMove(e: TouchEvent) {
if (props.disabled) return; if (props.disabled) return;
e.preventDefault();
const clientX = e.touches[0].clientX; const clientX = e.touches[0].clientX;
const value = calculateValue(clientX); const newValue = calculateValue(clientX);
updateValue(value);
if (props.range) {
const val = [...rangeValue.value];
val[activeThumbIndex.value] = newValue;
updateValue(val);
} else {
updateValue(newValue);
}
} }
// 触摸结束事件触发change事件 // 触摸结束事件触发change事件
function onTouchEnd() { function onTouchEnd() {
if (props.disabled) return; if (props.disabled) return;
emit("change", value.value); if (props.range) {
emit("change", rangeValue.value);
} else {
emit("change", value.value);
}
} }
// 监听外部v-model的变化保持内部value同步 // 监听外部v-model的变化保持内部value同步(单值模式)
watch( watch(
computed(() => props.modelValue), computed(() => props.modelValue),
(val: number) => { (val: number) => {
if (val !== value.value) { if (val !== value.value) {
// 限制同步值在[min, max]区间
value.value = Math.max(props.min, Math.min(props.max, val)); value.value = Math.max(props.min, Math.min(props.max, val));
} }
}, },
{ immediate: true } { immediate: true }
); );
// 监听外部v-model:values的变化保持内部rangeValue同步范围模式
watch(
computed(() => props.values),
(val: number[]) => {
rangeValue.value = val.map((e) => {
return Math.max(props.min, Math.min(props.max, e));
});
},
{ immediate: true }
);
// 监听max的变化确保value不会超过max // 监听max的变化确保value不会超过max
watch( watch(
computed(() => props.max), computed(() => props.max),
(val: number) => { (val: number) => {
if (value.value > val) { if (props.range) {
updateValue(val); const rangeVal = rangeValue.value;
if (rangeVal[0] > val || rangeVal[1] > val) {
updateValue([Math.min(rangeVal[0], val), Math.min(rangeVal[1], val)]);
}
} else {
const singleVal = value.value;
if (singleVal > val) {
updateValue(val);
}
} }
}, },
{ { immediate: true }
immediate: true
}
); );
// 监听min的变化确保value不会小于min // 监听min的变化确保value不会小于min
watch( watch(
computed(() => props.min), computed(() => props.min),
(val: number) => { (val: number) => {
if (value.value < val) { if (props.range) {
updateValue(val); const rangeVal = rangeValue.value;
if (rangeVal[0] < val || rangeVal[1] < val) {
updateValue([Math.max(rangeVal[0], val), Math.max(rangeVal[1], val)]);
}
} else {
const singleVal = value.value;
if (singleVal < val) {
updateValue(val);
}
} }
}, },
{ { immediate: true }
immediate: true
}
);
watch(
computed(() => [props.showValue]),
() => {
nextTick(() => {
getTrackInfo();
});
}
); );
onMounted(() => { onMounted(() => {
watch(
computed(() => [props.showValue]),
() => {
nextTick(() => {
getTrackInfo();
});
}
);
getTrackInfo(); getTrackInfo();
}); });
</script> </script>
@@ -321,7 +470,7 @@ onMounted(() => {
} }
&__progress { &__progress {
@apply absolute top-0 left-0 h-full rounded-full; @apply absolute top-0 h-full rounded-full;
@apply bg-primary-400; @apply bg-primary-400;
} }
@@ -330,6 +479,15 @@ onMounted(() => {
@apply bg-primary-500; @apply bg-primary-500;
transform: translateY(-50%); transform: translateY(-50%);
pointer-events: none; pointer-events: none;
z-index: 1;
&--min {
z-index: 2;
}
&--max {
z-index: 2;
}
} }
} }
</style> </style>

View File

@@ -12,6 +12,7 @@ export type ClSliderProps = {
className?: string; className?: string;
pt?: ClSliderPassThrough; pt?: ClSliderPassThrough;
modelValue?: number; modelValue?: number;
values?: number[];
min?: number; min?: number;
max?: number; max?: number;
step?: number; step?: number;
@@ -19,4 +20,5 @@ export type ClSliderProps = {
blockSize?: number; blockSize?: number;
trackHeight?: number; trackHeight?: number;
showValue?: boolean; showValue?: boolean;
range?: boolean;
}; };