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

vue实现评分星星

2026-02-18 20:50:57VUE

vue实现评分星星

vue实现评分星星

实现评分星星组件

在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中实现星级评分通常需要创建一个可交互的组件,允许用户点击或悬停选择评分。核心逻辑包括渲染星星图标、处理用户交互以及动态更新评分状态。 使用Font Awesome图标…

vue实现批量select评分

vue实现批量select评分

Vue 实现批量 Select 评分 在 Vue 中实现批量 Select 评分功能,可以通过动态生成评分选项并结合 v-model 实现数据绑定。以下是一个完整的实现示例: 基础实现 创建可复用的…

react实现评分功能

react实现评分功能

实现评分功能的方法 在React中实现评分功能可以通过多种方式完成,以下是一些常见的实现方法。 使用第三方库 使用现成的评分组件库可以快速实现功能,例如react-rating或react-star…

js 实现星星打分

js 实现星星打分

实现星星打分功能 使用HTML、CSS和JavaScript可以创建一个交互式的星星打分系统。以下是一个完整的实现方法: HTML结构 <div class="star-rating">…

vue实现评分

vue实现评分

实现评分功能的步骤 在Vue中实现评分功能可以通过多种方式完成,以下是一种常见的实现方法: 创建评分组件 使用v-for循环生成评分星星,并通过v-model绑定评分值: <templat…

css制作星星

css制作星星

使用CSS制作星星 在CSS中,可以通过多种方式制作星星效果,包括使用纯CSS形状、Unicode字符或SVG。以下是几种常见的方法: 使用Unicode字符 最简单的星星可以通过Unicode字符…