当前位置:首页 > VUE

vue实现评分星星

2026-03-10 10:10:53VUE

Vue 实现评分星星

方法一:使用 v-for 循环生成星星

通过 v-for 循环生成星星图标,利用动态绑定类名实现选中和未选中状态。

<template>
  <div>
    <span 
      v-for="star in 5" 
      :key="star" 
      @click="setRating(star)"
      :class="['star', { 'active': star <= rating }]"
    >
      ★
    </span>
    <p>当前评分: {{ rating }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rating: 0
    }
  },
  methods: {
    setRating(star) {
      this.rating = star
    }
  }
}
</script>

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

方法二:使用组件实现可复用评分

将评分功能封装为独立组件,便于复用。

<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star" 
      @click="rate(star)"
      @mouseover="hoverRating = star"
      @mouseleave="hoverRating = 0"
      :class="['star', { 'filled': star <= currentRating || star <= hoverRating }]"
    >
      ★
    </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: {
    rate(star) {
      this.currentRating = star
      this.$emit('rated', star)
    }
  }
}
</script>

<style>
.star-rating {
  display: inline-block;
}
.star {
  font-size: 24px;
  color: #ddd;
  cursor: pointer;
  transition: color 0.2s;
}
.star.filled {
  color: #ffcc00;
}
</style>

方法三:使用 SVG 实现更精美的星星

使用 SVG 绘制星星,可以获得更好的视觉效果。

<template>
  <div class="svg-star-rating">
    <svg 
      v-for="star in 5" 
      :key="star" 
      @click="setRating(star)"
      :class="{ 'active': star <= rating }"
      width="24" 
      height="24" 
      viewBox="0 0 24 24"
    >
      <path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/>
    </svg>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rating: 0
    }
  },
  methods: {
    setRating(star) {
      this.rating = star
    }
  }
}
</script>

<style>
.svg-star-rating svg {
  fill: #ddd;
  cursor: pointer;
  margin-right: 5px;
}
.svg-star-rating svg.active {
  fill: #ffcc00;
}
</style>

方法四:实现半星评分

通过计算实现半星评分功能,提供更精确的评分体验。

vue实现评分星星

<template>
  <div class="half-star-rating">
    <div 
      v-for="star in 5" 
      :key="star" 
      class="star-container"
      @mousemove="setHoverValue($event, star)"
      @mouseleave="hoverValue = 0"
      @click="setRating(star)"
    >
      <div class="star-background">★</div>
      <div 
        class="star-foreground" 
        :style="{ width: getStarWidth(star) }"
      >★</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rating: 0,
      hoverValue: 0,
      hoverPosition: 0
    }
  },
  methods: {
    setHoverValue(event, star) {
      const rect = event.target.getBoundingClientRect()
      this.hoverPosition = (event.clientX - rect.left) / rect.width
      this.hoverValue = star - 0.5 + this.hoverPosition
    },
    setRating(star) {
      this.rating = star - 0.5 + this.hoverPosition
    },
    getStarWidth(star) {
      const value = this.hoverValue || this.rating
      const starValue = value - (star - 1)
      return `${Math.min(1, Math.max(0, starValue)) * 100}%`
    }
  }
}
</script>

<style>
.half-star-rating {
  display: flex;
}
.star-container {
  position: relative;
  width: 24px;
  height: 24px;
  cursor: pointer;
}
.star-background, .star-foreground {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 24px;
  line-height: 1;
}
.star-background {
  color: #ddd;
}
.star-foreground {
  color: gold;
  overflow: hidden;
  white-space: nowrap;
}
</style>

这些方法提供了从简单到复杂的 Vue 评分星星实现方案,可以根据项目需求选择合适的实现方式。

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

相关文章

vue实现星星

vue实现星星

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

vue实现星级评分

vue实现星级评分

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

vue实现批量select评分

vue实现批量select评分

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

react实现星星评分

react实现星星评分

实现星星评分组件 使用React实现一个可交互的星星评分组件,可以通过状态管理用户选择的评分值。 import React, { useState } from 'react'; const St…

react实现评分功能

react实现评分功能

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

js 实现评分

js 实现评分

实现评分功能(五星评分) 在JavaScript中实现五星评分功能,可以通过HTML、CSS和JavaScript结合完成。以下是一个常见的实现方式: HTML部分 <div class="…