当前位置:首页 > VUE

vue实现星星

2026-01-12 09:12:57VUE

Vue 实现星星评分功能

在 Vue 中实现星星评分功能可以通过组件化方式完成,以下是具体实现方法:

vue实现星星

基础实现(静态评分)

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

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

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

.star-rating span.active {
  color: gold;
}
</style>

进阶实现(支持半星)

<template>
  <div class="star-rating">
    <span 
      v-for="star in 5" 
      :key="star"
      @mouseover="hoverRating = star"
      @mouseleave="hoverRating = 0"
      @click="setRating(star)"
    >
      <span 
        v-if="star <= currentRating || (hoverRating && star <= hoverRating)"
        style="color: gold;"
      >
        ★
      </span>
      <span v-else style="color: #ccc;">★</span>
    </span>
  </div>
</template>

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

封装为可复用组件

<!-- StarRating.vue -->
<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star"
      @click="rate(star)"
      @mouseover="hover(star)"
      @mouseleave="hover(0)"
    >
      <span :style="getStarStyle(star)">★</span>
    </span>
    <span v-if="showRating">{{ currentRating }} / {{ maxStars }}</span>
  </div>
</template>

<script>
export default {
  props: {
    rating: {
      type: Number,
      default: 0
    },
    maxStars: {
      type: Number,
      default: 5
    },
    showRating: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      currentRating: this.rating,
      hoverRating: 0
    }
  },
  methods: {
    rate(star) {
      this.currentRating = star
      this.$emit('rating-changed', star)
    },
    hover(star) {
      this.hoverRating = star
    },
    getStarStyle(star) {
      const color = star <= (this.hoverRating || this.currentRating) 
        ? 'gold' 
        : '#ccc'
      return { color, fontSize: '24px', cursor: 'pointer' }
    }
  }
}
</script>

使用组件示例

<template>
  <div>
    <star-rating 
      :rating="3" 
      @rating-changed="updateRating"
    />
    <p>当前评分: {{ userRating }}</p>
  </div>
</template>

<script>
import StarRating from './StarRating.vue'

export default {
  components: {
    StarRating
  },
  data() {
    return {
      userRating: 3
    }
  },
  methods: {
    updateRating(rating) {
      this.userRating = rating
    }
  }
}
</script>

这些实现方法可以根据具体需求进行调整,例如添加动画效果、自定义星星图标或与后端API集成等功能。

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

相关文章

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template>…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="con…

vue实现表单

vue实现表单

Vue 表单实现方法 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定。适用于 input、textarea、select 等元素。 <template> <…

vue模版实现

vue模版实现

Vue 模板实现方法 Vue 模板是 Vue.js 的核心特性之一,用于声明式地将 DOM 绑定至底层 Vue 实例的数据。以下是几种常见的 Vue 模板实现方式: 单文件组件(SFC) 使用 .v…

vue实现listview

vue实现listview

Vue 实现 ListView 的方法 基础列表渲染 使用 v-for 指令可以轻松实现基础列表渲染。在 Vue 中,v-for 用于遍历数组或对象,生成动态列表。 <template>…