当前位置:首页 > 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>

脚本部分

<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>

使用组件的方法

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

<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中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Obje…

vue实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript 图表…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安…