…">
当前位置:首页 > VUE

vue实现评分星星

2026-02-18 20:50:57VUE

实现评分星星组件

在Vue中实现评分星星功能可以通过自定义组件完成。以下是一个常见的实现方式:

<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star"
      @click="setRating(star)"
      @mouseover="hoverRating = star"
      @mouseleave="hoverRating = 0"
      :class="{
        'star': true,
        'active': star <= currentRating,
        'hover': star <= hoverRating && hoverRating !== 0
      }"
    >
      ★
    </span>
  </div>
</template>

<script>
export default {
  props: {
    maxStars: {
      type: Number,
      default: 5
    },
    initialRating: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentRating: this.initialRating,
      hoverRating: 0
    }
  },
  methods: {
    setRating(star) {
      this.currentRating = star;
      this.$emit('rating-changed', star);
    }
  }
}
</script>

<style>
.star-rating {
  font-size: 24px;
  cursor: pointer;
}

.star {
  color: #ccc;
  display: inline-block;
  transition: color 0.2s;
}

.star.active {
  color: #ffcc00;
}

.star.hover {
  color: #ffeb3b;
}
</style>

使用组件示例

<template>
  <div>
    <star-rating 
      :max-stars="5" 
      :initial-rating="3" 
      @rating-changed="handleRatingChange"
    />
    <p>当前评分: {{ selectedRating }}</p>
  </div>
</template>

<script>
import StarRating from './StarRating.vue';

export default {
  components: {
    StarRating
  },
  data() {
    return {
      selectedRating: 3
    }
  },
  methods: {
    handleRatingChange(rating) {
      this.selectedRating = rating;
    }
  }
}
</script>

可选功能扩展

  1. 半星评分:可以通过修改CSS和逻辑来实现半星评分效果
  2. 只读模式:添加一个prop来控制是否允许用户交互
  3. 自定义星星图标:使用SVG或字体图标替代简单的★字符
  4. 大小控制:通过prop控制星星的大小

半星评分实现示例

<template>
  <div class="star-rating">
    <span 
      v-for="star in stars" 
      :key="star.index"
      @click="setRating(star.value)"
      @mouseover="hoverRating = star.value"
      @mouseleave="hoverRating = 0"
      :class="{
        'star': true,
        'active': star.value <= currentRating,
        'hover': star.value <= hoverRating && hoverRating !== 0,
        'half': star.isHalf && star.value - 0.5 <= currentRating && currentRating < star.value
      }"
    >
      <span class="star-full">★</span>
      <span class="star-half">★</span>
    </span>
  </div>
</template>

<script>
export default {
  props: {
    maxStars: {
      type: Number,
      default: 5
    },
    initialRating: {
      type: Number,
      default: 0
    }
  },
  computed: {
    stars() {
      const stars = [];
      for (let i = 1; i <= this.maxStars; i++) {
        stars.push({
          index: i,
          value: i,
          isHalf: false
        });
        stars.push({
          index: i + 0.5,
          value: i,
          isHalf: true
        });
      }
      return stars;
    }
  },
  // 其余代码保持不变
}
</script>

<style>
.star {
  position: relative;
  display: inline-block;
  width: 24px;
  height: 24px;
  font-size: 24px;
  color: #ccc;
  cursor: pointer;
}

.star-full, .star-half {
  position: absolute;
  left: 0;
  top: 0;
}

.star-half {
  width: 50%;
  overflow: hidden;
}

.star.active .star-full,
.star.hover .star-full {
  color: #ffcc00;
}

.star.active .star-half,
.star.hover .star-half {
  color: #ffcc00;
}
</style>

这些实现方式提供了基本的评分星星功能,可以根据具体需求进行调整和扩展。

vue实现评分星星

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

相关文章

vue 实现评分

vue 实现评分

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

vue实现评分

vue实现评分

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

vue滑动评分实现

vue滑动评分实现

Vue 滑动评分实现 使用原生 HTML5 range 输入 在 Vue 中可以通过 <input type="range"> 快速实现滑动评分功能。这种方法不需要额外依赖库,适合简单场景…

vue实现评分案例

vue实现评分案例

实现评分组件的基本结构 在Vue中创建一个评分组件,可以使用v-for循环生成星星图标,并通过v-model或自定义事件实现双向绑定。以下是一个基于Font Awesome图标的实现示例: <…

vue实现星星评价

vue实现星星评价

实现星星评价组件 在Vue中实现星星评价功能,可以通过自定义组件完成。以下是一个完整的实现方案: 创建星星评价组件 新建一个StarRating.vue文件,包含以下代码: <templ…

vue实现星星评分原理

vue实现星星评分原理

实现星星评分的基本原理 星星评分通常通过显示一组星星图标,允许用户点击或悬停来选择评分值。Vue 的响应式特性使得实现这一功能变得简单,核心逻辑包括数据绑定、事件处理和动态样式控制。 数据绑定与渲…