You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.7 KiB

  1. <template>
  2. <div class="vote-results">
  3. <div v-for="opt in options" :key="opt.id" class="progressContainer">
  4. <!-- 居中显示的状态 -->
  5. <el-progress
  6. v-if="voteStatus"
  7. class="centered-progress"
  8. :percentage="100"
  9. :format="() => opt.content"
  10. :stroke-width="30"
  11. :text-inside="true"
  12. :color="colors[opt.id]"
  13. style="width: 300px;"
  14. @click="clickBtn(opt.id)"
  15. />
  16. <!-- 显示 count 于灰条最右侧的状态 -->
  17. <div v-else class="progressWrapper">
  18. <el-progress
  19. class="left-progress"
  20. :percentage="opt.count"
  21. :format="() => opt.content + ' √'"
  22. :stroke-width="30"
  23. :text-inside="true"
  24. :color="colors[opt.id]"
  25. />
  26. <!-- 绝对定位在灰色轨道的最右边 -->
  27. <span class="count-right">{{ opt.count }}</span>
  28. </div>
  29. </div>
  30. <el-button @click="totalStatus">确认</el-button>
  31. </div>
  32. </template>
  33. <script setup>
  34. import { ref } from 'vue'
  35. const voteStatus = ref(true)
  36. const totalStatus = () => {
  37. voteStatus.value = !voteStatus.value
  38. }
  39. const options = ref([
  40. { id: 1, content: '选择1', count: 20 },
  41. { id: 2, content: '选择2', count: 30 },
  42. { id: 3, content: '选择3', count: 40 },
  43. ])
  44. const colors = ref({})
  45. function genUniqueColors() {
  46. const used = new Set()
  47. options.value.forEach(opt => {
  48. let c
  49. do {
  50. c = '#' + Math.floor(Math.random() * 0x1000000)
  51. .toString(16)
  52. .padStart(6, '0')
  53. } while (used.has(c))
  54. used.add(c)
  55. colors.value[opt.id] = c
  56. })
  57. }
  58. genUniqueColors()
  59. const clickBtn = idx => {
  60. console.log('点击了:', idx)
  61. }
  62. </script>
  63. <style scoped>
  64. .progressContainer {
  65. width: 300px;
  66. margin-bottom: 20px;
  67. }
  68. /* 包含进度条的相对定位容器 */
  69. .progressWrapper {
  70. position: relative;
  71. width: 300px; /* 同进度条宽度 */
  72. }
  73. /* count 数字,绝对定位到右端,中间垂直对齐 */
  74. .count-right {
  75. position: absolute;
  76. right: 0;
  77. top: 50%;
  78. transform: translateY(-50%);
  79. margin-right: 4px; /* 根据需要微调,让数字在灰条内稍留空隙 */
  80. font-weight: bold;
  81. z-index: 1;
  82. }
  83. /* 居中状态 */
  84. .centered-progress :deep(.el-progress-bar__inner) {
  85. display: flex !important;
  86. align-items: center !important;
  87. justify-content: center !important;
  88. }
  89. .centered-progress :deep(.el-progress-bar__inner-text) {
  90. margin: 0 !important;
  91. width: auto !important;
  92. }
  93. /* 左对齐文本状态 */
  94. .left-progress :deep(.el-progress-bar__inner) {
  95. display: flex !important;
  96. align-items: center !important;
  97. justify-content: flex-start !important;
  98. padding-left: 8px !important;
  99. }
  100. .left-progress :deep(.el-progress-bar__inner-text) {
  101. margin: 0 !important;
  102. }
  103. </style>