当前位置:首页 > VUE

vue实现评分

2026-01-07 23:37:06VUE

Vue 实现评分功能

使用组件库实现

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

element-ui 为例:

<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 字符。

<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架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: &l…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在…

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

轮播vue实现

轮播vue实现

使用 Vue 实现轮播功能 基础轮播实现 安装依赖(如使用 Swiper): npm install swiper vue-awesome-swiper 组件代码示例: <templ…