当前位置:首页 > VUE

vue实现星星评分

2026-01-15 01:28:40VUE

Vue 实现星星评分

在 Vue 中实现星星评分功能,可以通过自定义组件或使用第三方库来完成。以下是两种常见实现方式:

自定义星星评分组件

创建一个可复用的星星评分组件,支持点击和悬停交互:

vue实现星星评分

<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star" 
      @click="rate(star)"
      @mouseover="hover(star)"
      @mouseleave="resetRating"
      :class="{ 'active': star <= currentRating }"
    >
      ★
    </span>
  </div>
</template>

<script>
export default {
  props: {
    maxStars: {
      type: Number,
      default: 5
    },
    initialRating: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentRating: this.initialRating,
      tempRating: 0
    };
  },
  methods: {
    rate(star) {
      this.currentRating = star;
      this.$emit('rated', star);
    },
    hover(star) {
      this.tempRating = this.currentRating;
      this.currentRating = star;
    },
    resetRating() {
      this.currentRating = this.tempRating;
    }
  }
};
</script>

<style scoped>
.star-rating {
  font-size: 24px;
}
.star-rating span {
  color: #ccc;
  cursor: pointer;
}
.star-rating .active {
  color: gold;
}
</style>

使用方法:

<star-rating 
  :max-stars="5" 
  :initial-rating="3" 
  @rated="handleRating"
/>

使用第三方库

安装 vue-star-rating 库:

vue实现星星评分

npm install vue-star-rating

使用示例:

<template>
  <star-rating 
    v-model:rating="rating" 
    :increment="0.5" 
    :star-size="30"
  />
</template>

<script>
import StarRating from 'vue-star-rating';

export default {
  components: {
    StarRating
  },
  data() {
    return {
      rating: 3.5
    };
  }
};
</script>

半星评分实现

如果需要支持半星评分,可以修改自定义组件:

<template>
  <div class="star-rating">
    <span 
      v-for="star in starsArray" 
      :key="star.index"
      @click="rate(star.value)"
      @mouseover="hover(star.value)"
      @mouseleave="resetRating"
      :class="getStarClass(star.value)"
    >
      {{ star.isHalf ? '½' : '★' }}
    </span>
  </div>
</template>

<script>
export default {
  props: {
    maxStars: {
      type: Number,
      default: 5
    },
    increment: {
      type: Number,
      default: 1
    },
    initialRating: {
      type: Number,
      default: 0
    }
  },
  computed: {
    starsArray() {
      const stars = [];
      for (let i = 1; i <= this.maxStars; i++) {
        stars.push({
          index: i,
          value: i,
          isHalf: false
        });
        if (this.increment === 0.5 && i < this.maxStars) {
          stars.push({
            index: i + 0.5,
            value: i + 0.5,
            isHalf: true
          });
        }
      }
      return stars;
    }
  },
  // 其余代码与前面示例相同
};
</script>

标签: 评分星星
分享给朋友:

相关文章

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…

vue实现星星

vue实现星星

Vue 实现星星评分功能 在 Vue 中实现星星评分功能可以通过组件化方式完成,以下是具体实现方法: 基础实现(静态评分) <template> <div class="st…

vue 实现评分

vue 实现评分

实现评分功能的方法 在Vue中实现评分功能可以通过多种方式完成,以下是几种常见的实现方法: 使用第三方库 使用现成的评分组件库可以快速实现评分功能,例如vue-star-rating或rate-it…

vue实现评分

vue实现评分

实现评分功能的基本方法 使用Vue实现评分功能可以通过多种方式完成,常见的方法包括基于组件、自定义指令或第三方库。以下是几种典型实现方案。 基于组件的评分实现 创建一个可复用的评分组件,利用v-f…