当前位置:首页 > VUE

vue实现星星

2026-01-07 07:11:14VUE

实现星星评分的Vue组件

使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案:

模板部分

<template>
  <div class="star-rating">
    <span 
      v-for="(star, index) in stars" 
      :key="index" 
      @click="rate(index + 1)"
      @mouseover="hover(index + 1)"
      @mouseleave="reset"
      :class="['star', { 'filled': index < currentRating }]"
    >
      ★
    </span>
  </div>
</template>

脚本部分

vue实现星星

<script>
export default {
  props: {
    maxRating: {
      type: Number,
      default: 5
    },
    initialRating: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      stars: Array(this.maxRating).fill(null),
      currentRating: this.initialRating,
      tempRating: 0
    }
  },
  methods: {
    rate(rating) {
      this.currentRating = rating
      this.$emit('rating-selected', rating)
    },
    hover(rating) {
      this.tempRating = rating
    },
    reset() {
      this.tempRating = 0
    }
  },
  computed: {
    filledStars() {
      return this.tempRating || this.currentRating
    }
  }
}
</script>

样式部分

<style scoped>
.star-rating {
  font-size: 24px;
  display: inline-flex;
}

.star {
  color: #ccc;
  cursor: pointer;
  transition: color 0.2s;
}

.star.filled {
  color: gold;
}

.star:hover {
  color: gold;
}
</style>

使用组件的方法

在父组件中引入并使用星星评分组件:

vue实现星星

<template>
  <div>
    <star-rating 
      :max-rating="5" 
      :initial-rating="3"
      @rating-selected="handleRating"
    />
    <p>当前评分: {{ selectedRating }}</p>
  </div>
</template>

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

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

实现半星功能

如果需要实现半星评分,可以修改组件如下:

<template>
  <div class="star-rating">
    <span 
      v-for="(star, index) in stars" 
      :key="index" 
      @click="rate(index + 0.5)"
      @dblclick="rate(index + 1)"
      @mouseover="hover(index + 0.5)"
      @mouseout="reset"
    >
      <span 
        class="star-half"
        :class="{ 'active': index + 0.5 <= filledStars }"
      ></span>
      <span 
        class="star"
        :class="{ 'active': index + 1 <= filledStars }"
      ></span>
    </span>
  </div>
</template>

实现只读星星

如果需要显示只读的星星评分:

<template>
  <div class="star-rating readonly">
    <span 
      v-for="(star, index) in stars" 
      :key="index" 
      :class="['star', { 'filled': index < currentRating }]"
    >
      ★
    </span>
  </div>
</template>

<script>
export default {
  props: {
    rating: {
      type: Number,
      required: true
    },
    maxRating: {
      type: Number,
      default: 5
    }
  },
  data() {
    return {
      stars: Array(this.maxRating).fill(null),
      currentRating: this.rating
    }
  }
}
</script>

<style scoped>
.readonly .star {
  cursor: default;
}
.readonly .star:hover {
  color: inherit;
}
</style>

这些实现方案覆盖了从基础到进阶的星星评分需求,可以根据具体项目要求选择合适的实现方式。

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

相关文章

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…