index.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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"></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. },
  34. data(){
  35. return{
  36. number:0
  37. }
  38. },
  39. watch:{
  40. // number(now,pre){
  41. // console.log(now,pre)
  42. // }
  43. },
  44. created() {
  45. this.number=this.star;
  46. },
  47. methods:{
  48. style(star,index){
  49. let status=0;
  50. if(index<=Math.floor(star)){
  51. status=100;
  52. }else if(Math.ceil(star)===index){
  53. status=(star-(Math.floor(star)))*100;
  54. status=parseFloat(status).toFixed(2);
  55. }else{
  56. status=0;
  57. }
  58. return status;
  59. },
  60. check(star,index){
  61. if(this.disable){
  62. return;
  63. }
  64. let status=this.style(star,index);
  65. let checked=!(100>status);
  66. let statNumber=star;
  67. if(checked){
  68. this.number=index-1;
  69. statNumber=index-1;
  70. }else{
  71. this.number=index;
  72. statNumber=index;
  73. }
  74. this.$emit('check',{
  75. checked,
  76. star:statNumber,
  77. index
  78. })
  79. }
  80. }
  81. }
  82. </script>
  83. <style scoped>
  84. .zhcx-star-wrap {
  85. display: flex;
  86. justify-content: flex-start;
  87. align-items: center;
  88. flex-wrap: wrap;
  89. }
  90. .zhcx-star-wrap .zhcx-star-item {
  91. position: relative;
  92. width: 50upx;
  93. height: 50upx;
  94. }
  95. .zhcx-star-wrap .zhcx-star-item:not(:first-child){
  96. margin-left: 10upx;
  97. }
  98. .zhcx-star-wrap .zhcx-star-item .zhcx-iconfont {
  99. font-size: 50upx;
  100. position: absolute;
  101. left: 0;
  102. top: 0;
  103. overflow: hidden;
  104. }
  105. .zhcx-star-wrap .zhcx-star-item .default {
  106. color: #c8c9cc;
  107. }
  108. .zhcx-star-wrap .zhcx-star-item .active {
  109. width: 100%;
  110. }
  111. </style>