当前位置:首页 > VUE

vue实现打星

2026-01-19 10:00:49VUE

实现打星评分功能

在Vue中实现打星评分功能可以通过自定义组件完成,利用v-for渲染星星图标,并通过事件绑定实现交互。以下是具体实现方法:

基础实现代码

创建StarRating.vue组件:

<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star"
      @click="setRating(star)"
      @mouseover="hoverRating(star)"
      @mouseleave="resetRating"
    >
      {{ star <= currentRating ? '★' : '☆' }}
    </span>
    <p v-if="showRating">{{ currentRating }} of {{ maxStars }}</p>
  </div>
</template>

<script>
export default {
  props: {
    maxStars: {
      type: Number,
      default: 5
    },
    initialRating: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentRating: this.initialRating,
      tempRating: 0
    }
  },
  computed: {
    showRating() {
      return this.currentRating > 0
    }
  },
  methods: {
    setRating(star) {
      this.currentRating = star
      this.$emit('rated', star)
    },
    hoverRating(star) {
      this.tempRating = this.currentRating
      this.currentRating = star
    },
    resetRating() {
      this.currentRating = this.tempRating
    }
  }
}
</script>

<style>
.star-rating {
  font-size: 24px;
  color: gold;
  cursor: pointer;
}
.star-rating span {
  margin-right: 5px;
}
</style>

使用方法

在父组件中引入并使用:

<template>
  <div>
    <StarRating 
      :max-stars="5" 
      :initial-rating="3" 
      @rated="handleRating"
    />
  </div>
</template>

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

export default {
  components: {
    StarRating
  },
  methods: {
    handleRating(rating) {
      console.log('用户评分:', rating)
    }
  }
}
</script>

进阶功能实现

实现半星评分功能:

<template>
  <div class="star-rating">
    <span 
      v-for="star in maxStars" 
      :key="star"
      @click="setRating(star)"
      @mousemove="setPreciseRating($event, star)"
    >
      <span class="star-background">☆</span>
      <span 
        class="star-foreground" 
        :style="{ width: getStarWidth(star) }"
      >★</span>
    </span>
  </div>
</template>

<script>
export default {
  props: {
    maxStars: {
      type: Number,
      default: 5
    },
    initialRating: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentRating: this.initialRating,
      preciseRating: 0
    }
  },
  methods: {
    setRating(star) {
      this.currentRating = star
      this.preciseRating = star
      this.$emit('rated', star)
    },
    setPreciseRating(event, star) {
      const starElement = event.target
      const rect = starElement.getBoundingClientRect()
      const percentage = (event.clientX - rect.left) / rect.width
      this.preciseRating = star - 1 + percentage
    },
    getStarWidth(star) {
      if (this.preciseRating >= star) return '100%'
      if (this.preciseRating > star - 1) return `${(this.preciseRating - (star - 1)) * 100}%`
      return '0%'
    }
  }
}
</script>

<style>
.star-rating {
  font-size: 24px;
  color: #ddd;
  cursor: pointer;
  display: inline-flex;
}
.star-rating > span {
  position: relative;
  margin-right: 5px;
}
.star-background {
  color: #ddd;
}
.star-foreground {
  color: gold;
  position: absolute;
  left: 0;
  top: 0;
  overflow: hidden;
}
</style>

使用第三方库

如需更复杂功能,可考虑使用现成库:

npm install vue-star-rating

使用示例:

vue实现打星

<template>
  <star-rating 
    :rating="rating"
    :increment="0.5"
    :max-rating="5"
    @rating-selected="setRating"
  />
</template>

<script>
import StarRating from 'vue-star-rating'

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

以上实现方案提供了从基础到进阶的打星评分功能,可根据项目需求选择适合的方案。自定义组件方式灵活性高,而第三方库则可以快速实现复杂交互效果。

标签: vue
分享给朋友:

相关文章

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…

vue 实现列表

vue 实现列表

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

vue实现复选

vue实现复选

Vue 实现复选框 在 Vue 中实现复选框可以通过 v-model 指令绑定数据,同时结合 input 元素的 type="checkbox" 属性来实现。以下是几种常见的实现方式: 单个复选框…

vue实现单词逆转

vue实现单词逆转

实现单词逆转的方法 在Vue中实现单词逆转可以通过多种方式完成,以下是几种常见的方法: 使用计算属性逆转单词 计算属性是Vue中处理数据逻辑的理想选择。可以通过计算属性来实现单词逆转: &…

vue实现肢体识别

vue实现肢体识别

Vue 中实现肢体识别的方法 在 Vue 项目中实现肢体识别通常需要结合第三方库或 API,例如 TensorFlow.js、MediaPipe 或 OpenCV。以下是几种常见的实现方式: 使用…