vue实现星级评分

1、vue实现星级评分

实现效果

在这里插入图片描述

实现代码
<template>
  <div>
      <ul class="ulcss">
          <li v-for="(item, index) in starList" :key="index">
              <img v-if="item.isShowStar" src="../assets/star.svg" alt="">
              <img v-else src="../assets/starEmpty.svg" alt="">
          </li>
      </ul>
  </div>
</template>

<script>
export default {
    name: 'Star',
    data() {
        return {
            starList: [
                {
                    isShowStar: false
                },
                {
                    isShowStar: false
                },
                {
                    isShowStar: false
                },
                {
                    isShowStar: false
                },
                {
                    isShowStar: false
                }
            ]
        }
    },
    props: {
        starNum: {
            type: Number,
            default: 0
        }
    },
    mounted() {
        for (var i = 0; i < this.starNum; i++) {
            this.starList[i].isShowStar = true
        }
    }
}
</script>

<style lang="less" scoped>
.ulcss {
    display: flex;
    flex-direction: row;
    li {
        list-style: none;
        margin-right: 12px;
    }
}
</style>

这期简单做的星级评分的小组件分享出来,希望可以帮助到大家

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是一个简单的 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 来实现星星的样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值