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

使用示例:

<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 实现目录功能 在 Vue 中实现目录功能通常需要动态生成目录结构,并支持点击跳转到对应内容区域。以下是几种常见实现方式: 基于滚动监听和动态生成 安装依赖(如需): npm inst…

vue异步组建实现原理

vue异步组建实现原理

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

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <templat…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…