当前位置:首页 > VUE

vue评分实现

2026-01-07 17:35:03VUE

Vue 评分组件实现方法

使用第三方库(如 Element UI)

安装 Element UI:

npm install element-ui

引入并注册组件:

import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

模板中使用:

vue评分实现

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

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

自定义评分组件

创建 StarRating.vue 组件:

<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 图标实现

创建 SvgStarRating.vue:

vue评分实现

<template>
  <div class="svg-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>

<script>
export default {
  // 同上一个组件的 script 部分
}
</script>

<style>
.svg-rating svg {
  fill: #ccc;
  cursor: pointer;
}
.svg-rating svg.active {
  fill: gold;
}
</style>

实现半星评分

修改自定义组件:

<template>
  <div class="half-star-rating">
    <div 
      v-for="star in maxStars" 
      :key="star" 
      class="star-container"
      @mouseover="hoverRating = star"
      @mouseleave="hoverRating = 0"
      @click="setRating(star)"
    >
      <div class="star-background">★</div>
      <div 
        class="star-foreground" 
        :style="{ width: (hoverRating || currentRating) >= star ? '100%' : '0%' }"
      >★</div>
    </div>
  </div>
</template>

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

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

使用 Vue 动画效果

为评分添加过渡动画:

<template>
  <transition-group name="star" tag="div" class="animated-rating">
    <span 
      v-for="star in maxStars" 
      :key="star" 
      @click="setRating(star)"
      :class="{ 'active': star <= currentRating }"
    >★</span>
  </transition-group>
</template>

<style>
.star-enter-active, .star-leave-active {
  transition: all 0.5s ease;
}
.star-enter, .star-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
</style>

双向绑定实现

实现 v-model 支持:

export default {
  props: ['value'],
  data() {
    return {
      internalValue: this.value
    }
  },
  watch: {
    value(newVal) {
      this.internalValue = newVal
    },
    internalValue(newVal) {
      this.$emit('input', newVal)
    }
  }
}

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

相关文章

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue实现tab换行

vue实现tab换行

Vue实现Tab切换的常见方法 使用Vue实现Tab切换功能可以通过多种方式完成,以下是几种常见的方法: 动态组件结合v-if或v-show 通过v-if或v-show控制不同Tab内容的显示…

vue实现购物按钮

vue实现购物按钮

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

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &l…

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vu…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mi…