首页 > 其他 > 详细

vue - 星星评分组件

时间:2019-07-20 22:47:17      阅读:85      评论:0      收藏:0      [点我收藏+]

输出:

技术分享图片

 

<template>
   <div class="Rating-gray">
     <i v-for="(item,index) in itemClasses" :key="index" class="fa" :class="item"></i>
   </div>
</template>

<script>
const LENGTH = 5;

const CLS_ON = "fa-star";
const CLS_HALF = "fa-star-half-empty";
const CLS_OF = "fa-star-0";

export default {
  name:"Rating",
  props:{
    rating:Number
  },
  computed:{
    itemClasses(){
      let result = [];
      // 如 4.8 对分数进行处理, 向下取0.5的倍数
      let score = Math.floor(this.rating*2)/2;
      // 用来判断哪种星星的标准
      let integer = Math.floor(score);
      let hasDecimal = score % 1 !== 0;
      for (let i = 0; i < integer; i++) {
        result.push(CLS_ON);
      };
      if(hasDecimal) {
        result.push(CLS_HALF);
      };
      while(result.length < LENGTH) {
        result.push(CLS_OF);
      };
      return result;
    }
  }
}
</script>

<style scoped>
.Rating-gray {
  margin-right: 1.066667vw;
  color: #ffbe00;
  display: inline-block;
}
</style>

 

vue - 星星评分组件

原文:https://www.cnblogs.com/-xiao/p/11219431.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!