当前位置:首页 > VUE

vue实现星星

2026-01-12 09:12:57VUE

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实现多选div

vue实现多选div

实现多选 DIV 的基本思路 在 Vue 中实现多选 DIV 功能,可以通过以下方法实现。主要思路是利用 v-model 绑定选中状态,结合 CSS 类名动态切换样式。 数据准备与模板结构 定义一个…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vu…

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncCom…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keywor…