[Vue]3行核心css代码的rate评分组件

一、评分

html

<img v-for="(star,index) in stars" :src="star.src" @click="rating(index)" style="width:24px;height:24px;vertical-align: middle;" />
<span class="scoreText"> {{starNum}} </span>

js


 //星星的图片路径
var starOffImg = "images/star_off.png";
var starOnImg = "images/star_on.png"; 

data:{
 starNum: 0,//分数 
 stars: [//星星数组
    {
       src: starOffImg,
        active: false
    }, {
        src: starOffImg,
        active: false
    }, {
        src: starOffImg,
        active: false
    },
    {
        src: starOffImg,
        active: false
    }, {
        src: starOffImg,
        active: false
    }
  ]
},
rating: function (index) {//评分
  var total = this.stars.length; //星星总数
   var idx = index + 1; //这代表选的第idx颗星-也代表应该显示的星星数量

   //进入if说明页面为初始状态
   if (this.starNum == 0) {
       this.starNum = idx;
       for (var i = 0; i < idx; i++) {
           this.stars[i].src = starOnImg;
           this.stars[i].active = true;
       }
   } else {
       //如果再次点击当前选中的星级-仅取消掉当前星级,保留之前的。
       if (idx == this.starNum) {
           for (var i = index; i < total; i++) {
               this.stars[i].src = starOffImg;
               this.stars[i].active = false;
           }
       }
       //如果小于当前最高星级,则直接保留当前星级
       if (idx < this.starNum) {
           for (var i = idx; i < this.starNum; i++) {
               this.stars[i].src = starOffImg;
               this.stars[i].active = false;
           }
       }
       //如果大于当前星级,则直接选到该星级
       if (idx > this.starNum) {
           for (var i = 0; i < idx; i++) {
               this.stars[i].src = starOnImg;
               this.stars[i].active = true;
           }
       }
       var count = 0; //计数器-统计当前有几颗星
       for (var i = 0; i < total; i++) {
           if (this.stars[i].active) {
               count++;
           }
       }
       this.starNum = count;
   }
}

评分:
在这里插入图片描述
点击后
在这里插入图片描述

二、显示评分

html

 <span v-for="(itemClass,index) in itemClasses" :class="itemClass" class="star-item"  track-by="index">  </span> 
 <span style="display:inline-block;width:50px;height:30px;line-height:30px">{{score}}</span>

css

.star-item {
  display: inline-block;
  background-repeat: no-repeat;
  width: 20px;
  height: 20px;
  margin-right: 10px;
  background-size: 100%;
}

.star-item.on {
  background-image: url(../images/star_on.png);
}

.star-item.half {
  background-image: url(../images/star_half.png);
}

.star-item.off {
  background-image: url(../images/star_off.png);
}

js

 var app = new Vue({
     el: '#app',
      data: { 
          score: 3.5
      },
      computed: { //计算属性
          itemClasses() {
              let result = [];
              let score = Math.floor(this.score * 2) / 2;
              let hasDecimal = score % 1 !== 0;
              let integer = Math.floor(score);
              for (let i = 0; i < integer; i++) {
                  result.push("on");
              }
              if (hasDecimal) {
                  result.push("half");
              }
              while (result.length < 5) {
                  result.push("off");
              }
              return result;
          }
      }
 })

显示:
在这里插入图片描述
参考链接: 3行核心css代码的rate评分组件

我还写了小程序实现的微信小程序——自定义评分组件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值