Vue實現Rate組件(星星評分)


這個功能就類似於某寶在購物之后,對商品進行評價

先看一下效果

那么實現這個功能也很簡單,新建一個my-rate組件,然后上代碼

  1 <template>
  2   <div class="rate" :class="{'disabled':disabled}">
  3     <span v-if="showText" class="text">{{curScore||score}}分</span>
  4     <div class="star-wrap">
  5       <i
  6         v-for="(i, index) in 5"
  7         :key="index"
  8         @mouseenter="disabled ? '' : curScore=i"
  9         @mouseleave="disabled ? '' : curScore=''"
 10         @click="disabled ? '' : setScore(i)"
 11         :class="getClass(i)"
 12       >
 13         <i v-if="disabled&&i==Math.floor(score)+1" class="icon-star" :style="'width:'+width"></i>
 14       </i>
 15     </div>
 16   </div>
 17 </template>
 18 <script>
 19 export default {
 20   name: 'MyRate',
 21   props: {
 22     // 分數,默認0,保留一位小數
 23     score: {
 24       type: Number,
 25       default: 0
 26     },
 27     // 是否只讀,默認false,鼠標點擊可以打分
 28     disabled: {
 29       type: Boolean,
 30       default: false
 31     },
 32     // 是否顯示分數,默認false
 33     showText: {
 34       type: Boolean,
 35       default: false
 36     }
 37   },
 38   data () {
 39     return {
 40       curScore: '',
 41       width: ''
 42     }
 43   },
 44   created: function () {
 45     this.getDecimal()
 46   },
 47   methods: {
 48     getClass (i) {
 49       if (this.curScore === '') {
 50         return i <= this.score ? 'icon-star' : 'icon-star-o'
 51       } else {
 52         return i <= this.curScore ? 'icon-star' : 'icon-star-o'
 53       }
 54     },
 55     getDecimal () {
 56       this.width = Number(this.score * 100 - Math.floor(this.score) * 100) + '%'
 57     },
 58     setScore (i) {
 59       this.$emit('update:score', i)
 60     }
 61   }
 62 }
 63 </script>
 64 <style lang="less" scoped>
 65 .rate{
 66   .text{
 67     font-size: 18px;
 68     color: #ff7f2c;
 69     font-weight: bold;
 70   }
 71   .star-wrap{
 72     line-height: 0;
 73     .icon-star-o{
 74       position: relative;
 75       width:8px;
 76       height: 8px;
 77       line-height: 0;
 78       display: inline-block;
 79       margin-right: 2px;
 80       background: url('../../public/gray.png') 0 0 ~'/' auto 100%;   // 這邊是沒有選中星星的圖片
 81       .icon-star{  82  position: absolute;  83         left:0;  84         top:0;
 85  }  86  }  87     .icon-star{  88  width: 8px;  89  height: 8px;  90       line-height: 0;  91       display: inline-block;  92       margin-right: 2px;  93       background: url('../../public/yellow.png') 0 0 ~'/' auto 100%; // 這邊是選中之后星星的圖片  94  }  95     i:last-child{  96       margin-right: 0px;  97  }  98  }  99 } 100 </style>

注意,你的項目必須支持less,這邊樣式用的less

在頁面調用,首先引入上面的組件

 1   <div class="hello">
 2     <h3>只讀,不顯示數字:</h3>
 3     <my-rate :score="1.5" :disabled="true"/>
 4     <h3>只讀,顯示數字:</h3>
 5     <my-rate :score="3.6" :disabled="true" showText/>
 6     <h3>只讀,顯示數字:</h3>
 7     <my-rate :score="4" :disabled="true" showText/>
 8     <h3>鼠標點擊評分,顯示數字:</h3>
 9     <my-rate :score.sync="curScore" showText/>
10   </div>

這樣就實現了一個封裝評分的組件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM