当前位置:首页 > VUE

vue实现评分

2026-01-07 23:37:06VUE

Vue 实现评分功能

使用组件库实现

安装 element-uiant-design-vue 这类 UI 库,它们已经内置了评分组件。

element-ui 为例:

vue实现评分

<template>
  <el-rate v-model="rating" :colors="colors"></el-rate>
</template>

<script>
export default {
  data() {
    return {
      rating: 3,
      colors: ['#99A9BF', '#F7BA2A', '#FF9900']
    }
  }
}
</script>

自定义评分组件

创建一个自定义的评分组件,使用 v-for 渲染星星图标,并通过点击事件更新评分。

<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star" 
      @click="setRating(star)"
      :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
    }
  },
  methods: {
    setRating(star) {
      this.currentRating = star
      this.$emit('rating-changed', star)
    }
  }
}
</script>

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

使用 SVG 图标

如果需要更灵活的样式,可以使用 SVG 图标代替 Unicode 字符。

vue实现评分

<template>
  <div class="star-rating">
    <svg 
      v-for="star in maxStars" 
      :key="star" 
      @click="setRating(star)"
      :class="{ 'active': star <= currentRating }"
      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>

半星评分实现

如果需要支持半星评分,可以稍微修改逻辑。

<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star" 
      @click="setRating(star)"
      @mousemove="setTempRating(star)"
      @mouseleave="resetTempRating"
    >
      <span 
        v-if="tempRating >= star || (!tempRating && currentRating >= star)"
        class="full"
      >★</span>
      <span 
        v-else-if="tempRating + 0.5 === star || (!tempRating && currentRating + 0.5 === star)"
        class="half"
      >½</span>
      <span v-else class="empty">★</span>
    </span>
  </div>
</template>

动态颜色评分

根据评分值动态改变星星颜色。

computed: {
  starColor() {
    return [
      '#ff4545', // 1星
      '#ffa534', // 2星
      '#ffe234', // 3星
      '#b7dd29', // 4星
      '#57e32c'  // 5星
    ][this.currentRating - 1] || '#ccc'
  }
}

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

相关文章

vue实现单页面

vue实现单页面

Vue 实现单页面应用(SPA) Vue.js 是一个渐进式框架,非常适合构建单页面应用(SPA)。SPA 的核心特点是整个应用只有一个 HTML 页面,通过动态加载内容实现页面切换,无需重新加载整个…

订单 vue实现

订单 vue实现

Vue 实现订单功能 数据模型设计 订单功能通常需要设计以下数据结构: // 订单数据结构 const order = { id: '', // 订单ID userId…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式…

vue实现购物按钮

vue实现购物按钮

Vue 购物按钮实现方法 基础按钮实现 使用 Vue 的模板语法创建基础按钮组件,绑定点击事件处理购物逻辑: <template> <button @click="addToC…

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…