封装星星评分

 在父组件中进行引用 子组件

<template>
  <div>
    <!-- 
      实现思路
      1.element icon图标 灰色黄色绝对定位
      2.设置评分对应分数赋值给黄色星星要显示的数量 黄色星星对应显示
      3.点击黄色星星 取消评分 两个相同事件传一个状态用来区分设置评分还是取消评分
      4.设置 赋值 取消评分 数量减一
      5.并且将我们选中的数量显示到我们的页面上
     -->
    <!-- input框事件,点击回车显示到将对应的星星显示到我们的页面中 -->
    <!-- 默认为五个 -->
    <rates :score.sync="curScore" />
  </div>
</template>

<script>
import rates from '@/components/rates.vue'
export default {
  components: { rates },
  data() {
    return {
      curScore: 5,
    }
  },
  created() {},
  mounted() {},
  methods: {},
}
</script>
<style scoped lang="scss"></style>

子组件中的数据

<template>
  <div class="rate">
    <span>得分:{{ inum }}分</span>
    <div class="star">
      <div class="box">
// 我们可以在class中改变所需要的样式
        <i
          class="el-icon-chat-round"
          name="star-o"
          v-for="(i, index) in score"
          :key="index"
          @click="setScore(i, 0)"
        ></i>
      </div>

      <div class="theSelected">
        <i
          class="ii el-icon-chat-line-round"
          @click="setScore(i, 1)"
          v-for="(i, index) in inum"
          :key="index"
        ></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    // 分数,默认5
    score: {
      type: Number,
      default: 5,
    },
  },

  data() {
    return {
      inum: 0,
    }
  },
  created() {},
  mounted() {},
  methods: {
    setScore(i, flag) {
      // i:分数
      // flag:0-->设置星星  1-->取消星星
      if (flag === 0) {
        this.inum = i
      } else {
        this.inum = i - 1
      }
    },
  },
}
</script>
<style scoped lang="scss">
.star {
  position: relative;
}
.box {
  position: absolute;
  top: 50px;
  left: 300px;
}
.theSelected {
  position: absolute;
  top: 50px;
  left: 300px;
}
.el-icon-chat-round,
.el-icon-chat-line-round {
  padding: 0 20px;
}
i {
  font-size: 40px;
}
.ii {
  color: rgb(173, 221, 192);
}
</style>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用vue的组件化思想来实现一个星级打分效果。 首先,在组件内部定义一个data属性,用来存储当前评分的分数。然后,在模板中使用循环语句来渲染五个星星,并根据当前分数来决定星星的颜色。 接着,为每个星星添加鼠标移入、移出、点击事件,用来改变当前分数并触发相应的回调函数。 最后,把组件封装成一个单独的.vue文件,可以在其他组件中引用。 下面是一个简单的例子: ```html <template> <div class="star-rating"> <span v-for="n in 5" :key="n" class="star" :class="{ 'filled': n <= currentRating }" @mouseenter="hoverRating(n)" @mouseleave="hoverRating(0)" @click="setRating(n)"> ★ </span> </div> </template> <script> export default { data() { return { currentRating: 0 } }, methods: { hoverRating(rating) { this.currentRating = rating }, setRating(rating) { this.currentRating = rating this.$emit('rating-changed', rating) } } } </script> <style> .star-rating { display: inline-block; font-size: 2em; width: 5em; text-align: center; } .star { color: #ccc; cursor: pointer; } .filled { color: gold; } </style> ``` 在父组件中,可以通过监听子组件的`rating-changed`事件来获取当前评分的分数,如下所示: ```html <template> <div> <star-rating @rating-changed="handleRatingChanged"></star-rating> <p>You rated this item {{ rating }} stars.</p> </div> </template> <script> import StarRating from './StarRating.vue' export default { components: { StarRating }, data() { return { rating: 0 } }, methods: { handleRatingChanged(rating) { this.rating = rating } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值