use-rate.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view class="use-rate dflex">
  3. <view class="pos-r margin-left-sm" v-for="(star,index) in stars" :key="index"
  4. :style="{ marginLeft: margin+'px' }" @click="onClick(index)">
  5. <view class="iconfont iconshoucang-01 ft-dark"></view>
  6. <view class="pos-a active" :style="{ width: star.activeWitch }">
  7. <view class="iconfont iconshoucang- ft-base"></view>
  8. </view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. props: {
  15. value: { //当前评分
  16. type: [Number, String],
  17. default: 0
  18. },
  19. margin: { //评分间距
  20. type: [Number, String],
  21. default: 0
  22. },
  23. max: { //最大评分
  24. type: [Number, String],
  25. default: 5
  26. },
  27. disabled: { //是否可点击
  28. type: [Boolean, String],
  29. default: false
  30. }
  31. },
  32. data() {
  33. // console.log('data')
  34. return {
  35. maxSync: this.max,
  36. valueSync: this.value
  37. }
  38. },
  39. computed: {
  40. stars() {
  41. const max = Number(this.maxSync) ? Number(this.maxSync) : 5
  42. const value = Number(this.valueSync) ? Number(this.valueSync) : 0
  43. const starList = []
  44. const floorValue = Math.floor(value)
  45. const ceilValue = Math.ceil(value)
  46. for (let i = 0; i < max; i++) {
  47. if (floorValue > i) {
  48. starList.push({
  49. activeWitch: '100%'
  50. })
  51. } else if (ceilValue - 1 === i) {
  52. starList.push({
  53. activeWitch: (value - floorValue) * 100 + '%'
  54. })
  55. } else {
  56. starList.push({
  57. activeWitch: '0'
  58. })
  59. }
  60. }
  61. return starList
  62. }
  63. },
  64. methods: {
  65. onClick(index) {
  66. if (this.disabled || this.disabled === 'true') {
  67. return
  68. }
  69. this.valueSync = index + 1
  70. this.$emit('change', {
  71. value: this.valueSync
  72. })
  73. }
  74. }
  75. }
  76. </script>
  77. <style lang="scss">
  78. .use-rate {
  79. .active {
  80. overflow: hidden;
  81. top: 0;
  82. }
  83. }
  84. </style>