当前位置:首页 > 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 中实现表格功能可以通过多种方式完成,以下介绍几种常见的方法: 使用原生 HTML 表格 最简单的表格可以通过 Vue 模板中的原生 HTML 表格元素实现: &l…

vue实现评论编辑

vue实现评论编辑

Vue 实现评论编辑功能 数据绑定与表单结构 使用 v-model 双向绑定评论内容到表单输入框,确保编辑时能显示原内容并同步修改。示例模板结构: <template> <di…

vue实现中台

vue实现中台

Vue 实现中台系统的关键步骤 技术选型与基础搭建 使用 Vue 3(Composition API)或 Vue 2(Options API)作为前端框架,搭配 Vue Router 实现路由管理,V…

vue实现文档分享

vue实现文档分享

Vue 实现文档分享功能 文档分享功能通常涉及文件上传、存储、生成分享链接以及权限控制等模块。以下是基于 Vue 的实现方案: 文件上传组件 使用 element-ui 或 ant-design-v…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '…

vue 移动端实现签名

vue 移动端实现签名

Vue 移动端签名实现方法 在移动端实现签名功能,可以通过HTML5的Canvas结合触摸事件完成。以下是具体实现步骤: 使用Canvas绘制签名 安装vue-signature-pad插件或直接使…