在vue-cli中实现类似电商的五星评价

用vue实现电商的五星评价

(通过修改元素的class名来切换背景图) 效果图如下:
这里写图片描述

这是两个图片,一个是空心一个是实心的:

     这里写图片描述       这里写图片描述
     

绑定鼠标点击两个事件:

使用鼠标进入mouseenter 鼠标离开mouseleave,代码如下:
:class=”{‘solid’:cur>=item}” 意思是当cur >= item的时候把 solid的 class 属性赋值过

<template>
 <div class="wrap">
   <div v-for="item in 5" @mouseenter="into(item)" @mouseleave="leave()" @click="flashed(item)" :class="{'solid':cur>=item}"></div>
 </div>
</template>
js代码:
<script>

  export default {

    data (){
      return {
        cur : 0,
        photo : false
      }
    },
    methods:{
      into(res){
        if(!this.photo){
          this.cur = res;
        }
      },
      leave(){
        if(!this.photo){
          this.cur = 0
        }
      },
      flashed(item){
        this.photo = true;
        this.cur = item;
      }
    }
  }
</script>
css代码:
<style scoped>
  .wrap{margin-top:100px;margin-left: 80px;}
  .wrap>div{
    width: 40px;
    height: 30px;
    float: left;
    background: url('../assets/images/xing_kong@2x.png') no-repeat;
    background-size: 28px;
  }

  .wrap .solid{
    background: url('../assets/images/xing_shi@2x.png') no-repeat;
    background-size: 28px;
  }
</style>
整体代码:
<template>
 <div class="wrap">
   <div v-for="item in 5" @mouseenter="into(item)" @mouseleave="leave()" @click="flashed(item)" :class="{'solid':cur>=item}"></div>
 </div>
</template>
<script>

  export default {
    data (){
      return {
        cur : 0,
        photo : false
      }
    },
    methods:{
      into(res){
        if(!this.photo){
          this.cur = res;
        }
      },
      leave(){
        if(!this.photo){
          this.cur = 0
        }
      },
      flashed(item){
        this.photo = true;
        this.cur = item;
      }
    }
  }
</script>

<style scoped>
  .wrap{margin-top:100px;margin-left: 80px;}
  .wrap>div{
    width: 40px;
    height: 30px;
    float: left;
    background: url('../assets/images/xing_kong@2x.png') no-repeat;
    background-size: 28px;
  }

  .wrap .solid{
    background: url('../assets/images/xing_shi@2x.png') no-repeat;
    background-size: 28px;
  }
</style>

这样效果就实现了!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值