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

实现只读星星

如果需要显示只读的星星评分:

vue实现星星

<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实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…

vue实现select

vue实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML select 元素 原生 HTML 的 <sel…

vue实现ftp

vue实现ftp

Vue 中实现 FTP 功能 Vue 本身是一个前端框架,无法直接操作 FTP 协议(FTP 通常需要后端支持)。以下是两种常见实现方式: 前端与后端配合实现 FTP 前端通过 API 与后端通信,…

vue实现卡片

vue实现卡片

Vue 实现卡片组件的方法 使用 Vue 实现卡片组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 在 Vue 单文件组件中,可以通过模板和样式直接实现卡片效果…

用vue实现滚动

用vue实现滚动

实现滚动的基本方法 在Vue中实现滚动可以通过多种方式完成,包括使用原生JavaScript、Vue指令或第三方库。以下是几种常见的方法: 使用ref和原生JavaScript 在模板中定义一个可滚…

vue实现滚动监听

vue实现滚动监听

滚动监听的基本实现 在Vue中实现滚动监听可以通过原生JavaScript的window.addEventListener或结合Vue的生命周期钩子完成。以下是一个基础示例: export defa…