Vue星级评分(为自己做个笔记)

页面:

<div>
  <span>Stars测评:{{starNum}}颗</span>
  <div>
    <img v-for="(star,index) in stars" v-bind:src="star.src" v-on:click="rating(index)"/>
  </div>
</div>

JS(script):

//vue找本地图片是找不到的 所以需要用到 require()
var starOffImg = require('../assets/122.png');
var starOnImg = require('../assets/221.png');
export default {
    name: "Evaluation",
  data(){
      return{
        starNum:0,
        stars:[{
          src:starOffImg,   
          active:false  //这个是在这最后计算总数的时候需要用到
        },{
          src:starOffImg,
          active:false
        },{
          src:starOffImg,
          active:false
        },{
          src:starOffImg,
          active:false
        },{
          src:starOffImg,
          active:false
        }]
      }
  },
  methods:{
  //这个方法是点击图片的时候才会掉  并不是一进页面就会走这个方法
    rating(index){
      var total = this.stars.length;//星星总数量
      var idx = index+1;//index 代表点击星星的下标  +1是因为下标是从0开始计数的  - idx代表要显示多少克星星

      //如果if=0说明是初始化状态
      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++){ //他会从点击的那个下标开始循环(是点亮那颗的下标作为基数,而不是 我们+1的idx)
              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;
      }
    }
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 Vue 星级评分组件的实现: ```html <template> <div class="rating"> <span v-for="(star, index) in stars" :class="['star', star]" :key="index" @click="select(index)"> </span> </div> </template> <script> export default { props: { value: { type: Number, default: 0 }, max: { type: Number, default: 5 } }, data() { return { stars: [] } }, mounted() { this.updateStars() }, methods: { updateStars() { this.stars = [] for (let i = 0; i < this.max; i++) { this.stars.push(i < this.value) } }, select(index) { this.value = index + 1 this.$emit('input', this.value) this.updateStars() } } } </script> <style> .star { display: inline-block; width: 24px; height: 24px; background: url(star.png) no-repeat; background-size: contain; cursor: pointer; } .star:hover, .star.active { background-position: 0 -24px; } </style> ``` 使用方式: ```html <template> <div> <h3>我的评分:{{ rating }}</h3> <star-rating v-model="rating"></star-rating> </div> </template> <script> import StarRating from './StarRating.vue' export default { components: { StarRating }, data() { return { rating: 3 } } } </script> ``` 其中,star.png 是一张包含两个星星的图片,第一个星星是默认状态,第二个星星是激活状态。 这个组件的实现很简单,通过 props 接收评分值和最大值,然后用一个数组来表示星星的激活状态,点击某个星星时更新评分值和星星状态,通过事件向父组件传递更新后的评分值。最后,用 CSS 来实现星星的样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值