您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

evaluate-star.vue 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <!-- 评价星级 -->
  2. <template>
  3. <view class="evaluate-star">
  4. <view class="evaluate_title" v-if="title">{{title}}</view>
  5. <view class="star-row">
  6. <view class="star-box" v-for="(item,index) in total" @click="changeRate(index)" :key="index" :style="style">
  7. <image class="star-img" :src="`${$imgUrl}common/whole_star.png`" v-if="whole-0.5>index" />
  8. <image :class="['star-img',whole-0.5>index?'star-hide':'']" :src="`${$imgUrl}common/half_star.png`"
  9. v-if="half && whole>index" />
  10. <image class="star-img" :src="`${$imgUrl}common/empty_star.png`" v-if="whole<=index" />
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script lang="ts" setup>
  16. import {
  17. onMounted,
  18. reactive,
  19. ref
  20. } from 'vue'
  21. const emits = defineEmits(['update:modelValue'])
  22. const props = defineProps({
  23. //双向绑定数据
  24. modelValue: {
  25. type: Number,
  26. default: 0
  27. },
  28. //评分总数
  29. total: {
  30. type: Number,
  31. default: 5
  32. },
  33. //评分名称
  34. title: {
  35. type: String,
  36. default: ''
  37. },
  38. //星星大小rpx
  39. size: {
  40. type: Number,
  41. default: 48
  42. }
  43. })
  44. const whole = ref(0)
  45. const half = ref(false)
  46. const sum = ref(0)
  47. let style = reactive({})
  48. onMounted(() => {
  49. style = {
  50. width: props.size + 'rpx',
  51. height: props.size + 'rpx',
  52. }
  53. })
  54. const changeRate = (e) => {
  55. var a = e++
  56. sum.value++
  57. if (sum.value === 1) {
  58. whole.value = e - 0.5
  59. half.value = true
  60. sum.value = -1
  61. } else {
  62. half.value = false
  63. whole.value = e++
  64. }
  65. emits('update:modelValue', whole.value)
  66. }
  67. </script>
  68. <style lang="scss">
  69. /* 点评星级 */
  70. .evaluate-star {
  71. width: 100%;
  72. display: flex;
  73. align-items: center;
  74. .evaluate_title {
  75. font-size: 28rpx;
  76. color: #333333;
  77. padding-right: 20rpx;
  78. }
  79. .star-row {
  80. display: flex;
  81. align-items: center;
  82. .star-box {
  83. width: 48rpx;
  84. height: 48rpx;
  85. margin-right: 16rpx;
  86. .star-img {
  87. width: 100%;
  88. height: 100%;
  89. }
  90. .star-hide {
  91. display: none;
  92. }
  93. }
  94. }
  95. }
  96. </style>