当前位置:首页 > VUE

vue滑动评分实现

2026-02-17 10:45:04VUE

实现滑动评分功能

在Vue中实现滑动评分功能可以通过自定义组件结合触摸或鼠标事件完成。以下是两种常见实现方式:

基于自定义滑块组件

创建一个可拖动的评分滑块,利用v-model实现双向绑定:

<template>
  <div class="rating-slider">
    <div class="track" ref="track" @mousemove="handleMove" @click="updateRating">
      <div class="thumb" :style="{ left: thumbPosition }" 
           @mousedown="startDrag" @touchstart="startDrag"></div>
    </div>
    <span>当前评分: {{ currentRating }}</span>
  </div>
</template>

<script>
export default {
  props: {
    value: { type: Number, default: 0 },
    max: { type: Number, default: 5 }
  },
  data() {
    return {
      isDragging: false,
      currentRating: this.value
    }
  },
  computed: {
    thumbPosition() {
      return `${(this.currentRating / this.max) * 100}%`
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      if (e.type === 'touchstart') {
        document.addEventListener('touchmove', this.handleMove)
        document.addEventListener('touchend', this.stopDrag)
      } else {
        document.addEventListener('mousemove', this.handleMove)
        document.addEventListener('mouseup', this.stopDrag)
      }
    },
    handleMove(e) {
      if (!this.isDragging) return
      const track = this.$refs.track
      const rect = track.getBoundingClientRect()
      const clientX = e.clientX || e.touches[0].clientX
      let pos = (clientX - rect.left) / rect.width
      pos = Math.max(0, Math.min(1, pos))
      this.currentRating = Math.round(pos * this.max * 10) / 10
    },
    stopDrag() {
      this.isDragging = false
      document.removeEventListener('mousemove', this.handleMove)
      document.removeEventListener('touchmove', this.handleMove)
      this.$emit('input', this.currentRating)
    },
    updateRating(e) {
      this.handleMove(e)
      this.stopDrag()
    }
  }
}
</script>

<style>
.rating-slider {
  width: 300px;
  margin: 20px;
}
.track {
  height: 10px;
  background: #eee;
  border-radius: 5px;
  position: relative;
  cursor: pointer;
}
.thumb {
  width: 20px;
  height: 20px;
  background: #42b983;
  border-radius: 50%;
  position: absolute;
  top: -5px;
  transform: translateX(-50%);
}
</style>

基于现有UI库

使用现成的Vue UI库可以快速实现评分功能,例如:

<template>
  <div>
    <!-- 使用Element UI的滑块 -->
    <el-slider v-model="rating" :max="5" :step="0.5" show-stops></el-slider>

    <!-- 或使用Vuetify的评分组件 -->
    <v-rating v-model="rating" half-increments hover></v-rating>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rating: 3.5
    }
  }
}
</script>

实现星形评分组件

创建一个视觉上更直观的星形评分组件:

vue滑动评分实现

<template>
  <div class="star-rating">
    <span v-for="star in maxStars" :key="star" 
          @click="setRating(star)" @mouseover="hoverRating = star"
          @mouseleave="hoverRating = 0"
          :class="{ 'active': star <= currentRating }">★</span>
  </div>
</template>

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

<style>
.star-rating span {
  font-size: 24px;
  color: #ccc;
  cursor: pointer;
  transition: color 0.2s;
}
.star-rating span.active {
  color: #ffd700;
}
.star-rating span:hover {
  color: #ffd700;
}
</style>

注意事项

  • 移动端适配需要考虑触摸事件处理
  • 可以通过CSS变量自定义颜色和大小
  • 半星评分可以通过计算小数部分实现
  • 性能优化时可考虑使用requestAnimationFrame处理拖动事件
  • 无障碍访问需要添加适当的ARIA属性

以上实现方式可根据具体需求选择或组合使用,自定义组件方式灵活性更高,而UI库组件可以快速集成但定制性有限。

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

相关文章

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue nexttick 实现

vue nexttick 实现

Vue nextTick 实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它主要用于确保在 DOM 更新完成后执行某些操作。 核心实现机制 ne…

vue 实现赋值

vue 实现赋值

Vue 实现赋值的方法 在 Vue 中,赋值操作通常涉及数据绑定和响应式更新。以下是几种常见的赋值方式: 直接赋值给 data 属性 在 Vue 实例的 data 对象中定义的属性可以直接通过 th…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…