uni-rate.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="uni-rate">
  3. <view class="uni-rate-icon" v-for="(star,index) in stars" :key="index" :style="{marginLeft:margin+'px'}" @click="onClick(index)">
  4. <uni-icon :size="size" :color="color" :type="isFill === false || isFill === 'false' ? 'star' : 'star-filled'"></uni-icon>
  5. <view class="uni-rate-icon-on" :style="{width:star.activeWitch}">
  6. <uni-icon :size="size" :color="activeColor" type="star-filled"></uni-icon>
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uniIcon from '../uni-icon/uni-icon.vue'
  13. export default {
  14. name: "uni-rate",
  15. components: {
  16. uniIcon
  17. },
  18. props: {
  19. isFill: { //星星的类型,是否镂空
  20. type: [Boolean, String],
  21. default: true
  22. },
  23. color: { //星星的颜色
  24. type: String,
  25. default: '#ececec'
  26. },
  27. activeColor: { //星星选中状态颜色
  28. type: String,
  29. default: '#ffca3e'
  30. },
  31. size: { //星星的大小
  32. type: [Number, String],
  33. default: 24
  34. },
  35. value: { //当前评分
  36. type: [Number, String],
  37. default: 0
  38. },
  39. max: { //最大评分
  40. type: [Number, String],
  41. default: 5
  42. },
  43. margin: { //星星的间距
  44. type: [Number, String],
  45. default: 0
  46. },
  47. disabled: { //是否可点击
  48. type: [Boolean, String],
  49. default: false
  50. }
  51. },
  52. data() {
  53. console.log('data')
  54. return {
  55. maxSync: this.max,
  56. valueSync: this.value
  57. }
  58. },
  59. computed: {
  60. stars() {
  61. const max = Number(this.maxSync) ? Number(this.maxSync) : 5
  62. const value = Number(this.valueSync) ? Number(this.valueSync) : 0
  63. const starList = []
  64. const floorValue = Math.floor(value)
  65. const ceilValue = Math.ceil(value)
  66. for (let i = 0; i < max; i++) {
  67. if (floorValue > i) {
  68. starList.push({
  69. activeWitch: '100%'
  70. })
  71. } else if (ceilValue - 1 === i) {
  72. starList.push({
  73. activeWitch: (value - floorValue) * 100 + '%'
  74. })
  75. } else {
  76. starList.push({
  77. activeWitch: '0'
  78. })
  79. }
  80. }
  81. return starList
  82. }
  83. },
  84. methods: {
  85. onClick(index) {
  86. if (this.disabled === true || this.disabled === 'true') {
  87. return
  88. }
  89. this.valueSync = index + 1
  90. this.$emit('change', {
  91. value: this.valueSync
  92. })
  93. }
  94. }
  95. }
  96. </script>
  97. <style lang="scss">
  98. .uni-rate {
  99. line-height: 0;
  100. font-size: 0;
  101. display: flex;
  102. flex-direction: row;
  103. &-icon {
  104. position: relative;
  105. line-height: 0;
  106. font-size: 0;
  107. display: inline-block;
  108. &-on {
  109. position: absolute;
  110. top: 0;
  111. left: 0;
  112. overflow: hidden;
  113. }
  114. }
  115. }
  116. </style>