index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="zhcx-star-wrap">
  3. <view class="zhcx-star-item" v-for="(item,index) in total" :key="index" @click="check(number,index+1)">
  4. <text class="default zhcx-iconfont" :class="icon" :style="{color:defaultcolor}"></text>
  5. <text class="active zhcx-iconfont" :class="icon" :style="{width:style(number,index+1)+'%',color:color}"></text>
  6. </view>
  7. </div>
  8. </template>
  9. <script>
  10. export default{
  11. name:'zhcx-star',
  12. props:{
  13. star:{
  14. type:[Number,String],
  15. default:0
  16. },
  17. total:{
  18. type:[Number],
  19. default:5
  20. },
  21. disable:{
  22. type:[Boolean],
  23. default:true
  24. },
  25. icon:{
  26. type:[String],
  27. default:"zhcx-icon-shoucang"
  28. },
  29. color:{
  30. type:[String],
  31. default:"red"
  32. },
  33. defaultcolor:{
  34. type:[String],
  35. default:"#c8c9cc"
  36. }
  37. },
  38. data(){
  39. return{
  40. number:0
  41. }
  42. },
  43. watch:{
  44. // number(now,pre){
  45. // console.log(now,pre)
  46. // }
  47. },
  48. created() {
  49. this.number=this.star;
  50. },
  51. methods:{
  52. style(star,index){
  53. let status=0;
  54. if(index<=Math.floor(star)){
  55. status=100;
  56. }else if(Math.ceil(star)===index){
  57. status=(star-(Math.floor(star)))*100;
  58. status=parseFloat(status).toFixed(2);
  59. }else{
  60. status=0;
  61. }
  62. return status;
  63. },
  64. check(star,index){
  65. if(this.disable){
  66. return;
  67. }
  68. let status=this.style(star,index);
  69. let checked=!(100>status);
  70. let statNumber=star;
  71. if(checked){
  72. this.number=index-1;
  73. statNumber=index-1;
  74. }else{
  75. this.number=index;
  76. statNumber=index;
  77. }
  78. this.$emit('check',{
  79. checked,
  80. star:statNumber,
  81. index
  82. })
  83. }
  84. }
  85. }
  86. </script>
  87. <style scoped>
  88. .zhcx-star-wrap {
  89. display: flex;
  90. justify-content: flex-start;
  91. align-items: center;
  92. flex-wrap: wrap;
  93. }
  94. .zhcx-star-wrap .zhcx-star-item {
  95. position: relative;
  96. width: 50upx;
  97. height: 50upx;
  98. }
  99. .zhcx-star-wrap .zhcx-star-item:not(:first-child){
  100. margin-left: 10upx;
  101. }
  102. .zhcx-star-wrap .zhcx-star-item .zhcx-iconfont {
  103. font-size: 50upx;
  104. position: absolute;
  105. left: 0;
  106. top: 0;
  107. overflow: hidden;
  108. }
  109. .zhcx-star-wrap .zhcx-star-item .default {
  110. color: #c8c9cc;
  111. }
  112. .zhcx-star-wrap .zhcx-star-item .active {
  113. width: 100%;
  114. }
  115. </style>