原生js星星评分源码

html:
<div id="fiveStars">
        <div>到场时间:<img v-for="(star,index) in stars.list" :src="star.src" @click="rating(index, stars)"  /></div>   // index,下标 stars 总的图片
</div>
 
引入图片路径:
//黑星星的路径
const starOff ='../../../static/img/wjx-old.png';
//亮星星的路径
const starOn='../../../static/img/wjx-new.png';
 
data:设置
stars: {
      list: [
                            {src: starOff, active: false},           // src: 图片星星,默认灰色,  active,添加
                            {src: starOff, active: false},
                            {src: starOff, active: false},
                            {src: starOff, active: false},
                            {src: starOff, active: false},
       ],
       starNum:0           // 点击星星的次数
},
 
  
 
 
methods:
rating(index, stars){
                let total = stars.list.length;//星星总数
                let idx = index + 1//代表应该显示的星星的数量
                if(stars.starNum ==0){ //只点了一个星星
                    stars.starNum =idx
                    for(var i=0;i<idx;i++){
                    stars.list[i].src=starOn;
                    stars.list[i].active=true;
                    }
                }else{
          //如果再次点击当前选中的星级-仅取消掉当前星级,保留之前的。
                    if(idx == stars.starNum){
                        for(var i=idx;i<total;i++){
                            stars.list[i].src=starOff;
                            stars.list[i].active=false;
                        }
                    }
          //如果小于当前最高星级,则直接保留当前星级
                    if(idx <stars.starNum){
                        for(var i=idx;i<stars.starNum;i++){
                            stars.list[i].src=starOff;
                            stars.list[i].active=false;
                        }
                    }
          //如果大于当前星级,则直接选到该星级
                    if(idx > stars.starNum){
                        for(var i=0;i<idx;i++){
                            stars.list[i].src=starOn;
                            stars.list[i].active=true;
                        }
                    }
                    let count =0;//当前几颗星
                    for(var i=0;i<total;i++){
                        if(stars.list[i].active){
                            count++;
                        }
                    }
                    stars.starNum =count;
          console.log(stars.starNum)
        }
},

 

转载于:https://www.cnblogs.com/panax/p/11024244.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值