当前位置:首页 > VUE

vue实现星星

2026-03-27 07:21:35VUE

使用Vue实现星星评分组件

方法一:基础实现

创建Vue组件,使用v-for循环生成星星图标,通过点击事件切换选中状态

<template>
  <div class="star-rating">
    <span 
      v-for="(star, index) in stars" 
      :key="index"
      @click="rate(index)"
      :class="{ 'active': index < currentRating }"
    >
      ★
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      stars: Array(5).fill('★'),
      currentRating: 0
    }
  },
  methods: {
    rate(index) {
      this.currentRating = index + 1
    }
  }
}
</script>

<style>
.star-rating span {
  font-size: 2rem;
  color: #ccc;
  cursor: pointer;
}
.star-rating span.active {
  color: gold;
}
</style>

方法二:支持半星评分

通过计算属性处理半星显示逻辑,使用CSS实现半星效果

<template>
  <div class="star-rating">
    <span 
      v-for="(star, index) in stars" 
      :key="index"
      @mouseover="hoverRating = index + 1"
      @mouseleave="hoverRating = 0"
      @click="setRating(index + 1)"
    >
      <span class="star-background">★</span>
      <span 
        class="star-foreground" 
        :style="{ width: getStarWidth(index) }"
      >★</span>
    </span>
  </div>
</template>

<script>
export default {
  props: {
    value: Number,
    maxStars: {
      type: Number,
      default: 5
    }
  },
  data() {
    return {
      hoverRating: 0,
      stars: Array(this.maxStars).fill('★')
    }
  },
  computed: {
    currentRating() {
      return this.hoverRating || this.value
    }
  },
  methods: {
    setRating(rating) {
      this.$emit('input', rating)
    },
    getStarWidth(index) {
      const rating = this.currentRating
      if (rating >= index + 1) return '100%'
      if (rating > index) return `${(rating - index) * 100}%`
      return '0%'
    }
  }
}
</style>

方法三:使用第三方图标库

结合Font Awesome等图标库实现更精美的星星效果

<template>
  <div class="star-rating">
    <i 
      v-for="index in maxStars"
      :key="index"
      :class="[
        'fas',
        index <= currentRating ? 'fa-star' : 'fa-star-o'
      ]"
      @click="setRating(index)"
      @mouseover="hoverRating = index"
      @mouseleave="hoverRating = 0"
    ></i>
  </div>
</template>

<script>
export default {
  props: {
    value: Number,
    maxStars: {
      type: Number,
      default: 5
    }
  },
  data() {
    return {
      hoverRating: 0
    }
  },
  computed: {
    currentRating() {
      return this.hoverRating || this.value
    }
  },
  methods: {
    setRating(rating) {
      this.$emit('input', rating)
    }
  }
}
</script>

注意事项

vue实现星星

  1. 组件应支持双向绑定,使用v-model指令
  2. 可通过props自定义星星数量、大小和颜色
  3. 添加适当的过渡效果提升用户体验
  4. 考虑添加disabled状态防止误操作
  5. 对于移动端设备,需要添加触摸事件支持

标签: 星星vue
分享给朋友:

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…