cl-progress.uvue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <view class="cl-progress">
  3. <view class="cl-progress__outer" :style="outerStyle">
  4. <view class="cl-progress__inner" :style="innerStyle"></view>
  5. </view>
  6. <slot name="text">
  7. <cl-rolling-number
  8. :model-value="value"
  9. :pt="{
  10. className: 'w-[100rpx] text-center'
  11. }"
  12. unit="%"
  13. v-if="showText"
  14. >
  15. </cl-rolling-number>
  16. </slot>
  17. </view>
  18. </template>
  19. <script lang="ts" setup>
  20. import { computed, getCurrentInstance, onMounted, ref, watch } from "vue";
  21. import { parseRpx } from "@/cool";
  22. defineOptions({
  23. name: "cl-progress"
  24. });
  25. const props = defineProps({
  26. // 数值
  27. value: {
  28. type: Number,
  29. default: 0
  30. },
  31. // 线条宽度
  32. strokeWidth: {
  33. type: Number,
  34. default: 12
  35. },
  36. // 是否显示文本
  37. showText: {
  38. type: Boolean,
  39. default: true
  40. },
  41. // 线条颜色
  42. color: {
  43. type: String,
  44. default: null
  45. },
  46. // 底色
  47. unColor: {
  48. type: String,
  49. default: null
  50. }
  51. });
  52. const { proxy } = getCurrentInstance()!;
  53. // 当前值
  54. const value = ref(0);
  55. // 进度条宽度
  56. const width = ref(0);
  57. // 外层样式
  58. const outerStyle = computed(() => {
  59. const style = new Map<string, string>();
  60. style.set("height", parseRpx(props.strokeWidth));
  61. if (props.unColor != null) {
  62. style.set("backgroundColor", props.unColor!);
  63. }
  64. return style;
  65. });
  66. // 内层样式
  67. const innerStyle = computed(() => {
  68. const style = new Map<string, string>();
  69. style.set("width", `${(value.value / 100) * width.value}px`);
  70. if (props.color != null) {
  71. style.set("backgroundColor", props.color!);
  72. }
  73. return style;
  74. });
  75. // 获取进度条宽度
  76. function getWidth() {
  77. uni.createSelectorQuery()
  78. .in(proxy)
  79. .select(".cl-progress__outer")
  80. .boundingClientRect((node) => {
  81. width.value = (node as NodeInfo).width ?? 0;
  82. })
  83. .exec();
  84. }
  85. onMounted(() => {
  86. watch(
  87. computed(() => props.value),
  88. (val: number) => {
  89. getWidth();
  90. setTimeout(() => {
  91. if (val > 100) {
  92. value.value = 100;
  93. } else if (val < 0) {
  94. value.value = 0;
  95. } else {
  96. value.value = val;
  97. }
  98. }, 10);
  99. },
  100. {
  101. immediate: true
  102. }
  103. );
  104. });
  105. </script>
  106. <style lang="scss" scoped>
  107. .cl-progress {
  108. @apply flex flex-row items-center w-full rounded-md;
  109. &__outer {
  110. @apply bg-surface-100 relative rounded-md;
  111. flex: 1;
  112. }
  113. &__inner {
  114. @apply h-full absolute top-0 left-0 z-10 rounded-md;
  115. @apply bg-primary-500;
  116. transition-property: width;
  117. transition-duration: 0.5s;
  118. }
  119. }
  120. </style>