ProgressBar.vue
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<template>
<div :style="barWrapperStyle" class="progress-bar-wrapper">
<div :style="barStyle" class="progress-bar"></div>
</div>
</template>
<script lang="ts" setup name="ProgressBar">
import { computed, defineProps } from "vue";
// 定义 Props 类型
interface ProgressBarProps {
value: number;
max: number;
color?: string;
grayLevelColor?: string;
height?: string;
direction?: 'left' | 'right';
innerRadius?: boolean;
borderRadius?: string;
}
// 接收 Props
const props = withDefaults(defineProps<ProgressBarProps>(), {
value: 20,
max: 100,
color: "#24de73",
grayLevelColor: "#f5f5f5",
direction: "left",
height: "6px",
innerRadius: true,
borderRadius: '3px'
});
// 计算进度百分比
const progress = computed(() => (props.value / props.max) * 100);
// 进度条外层样式,支持根据方向自定义
const barWrapperStyle = computed(() => ({
overflow: "hidden",
height: props.height,
borderRadius: props.borderRadius,
backgroundColor: props.grayLevelColor, // 背景颜色
justifyContent: props.direction === 'left' ? 'flex-start' : 'flex-end'
}));
// 进度条样式
const barStyle = computed(() => ({
width: `${progress.value}%`,
backgroundColor: props.color,
borderRadius: props.innerRadius ? props.borderRadius : '',
transition: "width 0.3s, height 0.3s", // 动画过渡效果
}));
</script>
<style scoped>
.progress-bar-wrapper {
width: 100%;
display: flex;
}
.progress-bar {
height: 100%;
}
</style>