当前位置:首页 > VUE

vue滑动评分实现

2026-03-08 23:04:19VUE

Vue 滑动评分实现方法

使用第三方库(如vue-rate)

安装依赖:

npm install vue-rate --save

在组件中使用:

vue滑动评分实现

<template>
  <vue-rate v-model="rating" :length="5" :size="30" active-color="#FFD700"/>
</template>

<script>
import VueRate from 'vue-rate';
export default {
  components: { VueRate },
  data() {
    return {
      rating: 3
    }
  }
}
</script>

自定义滑动评分组件

创建可拖动滑块组件:

vue滑动评分实现

<template>
  <div class="rating-container">
    <div class="rating-track" ref="track" @click="handleClick">
      <div class="rating-thumb" 
           :style="{ left: thumbPosition + 'px' }"
           @mousedown="startDrag"
           @touchstart="startDrag">
      </div>
    </div>
    <div class="rating-value">{{ currentRating.toFixed(1) }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentRating: 0,
      isDragging: false,
      maxRating: 5,
      trackWidth: 200
    }
  },
  computed: {
    thumbPosition() {
      return (this.currentRating / this.maxRating) * this.trackWidth
    }
  },
  methods: {
    handleClick(e) {
      const rect = this.$refs.track.getBoundingClientRect()
      this.currentRating = Math.min(
        this.maxRating, 
        Math.max(0, (e.clientX - rect.left) / rect.width * this.maxRating)
      )
      this.$emit('input', this.currentRating)
    },
    startDrag() {
      this.isDragging = true
      document.addEventListener('mousemove', this.handleDrag)
      document.addEventListener('touchmove', this.handleDrag)
      document.addEventListener('mouseup', this.stopDrag)
      document.addEventListener('touchend', this.stopDrag)
    },
    handleDrag(e) {
      if (!this.isDragging) return
      const clientX = e.clientX || e.touches[0].clientX
      this.handleClick({ clientX })
    },
    stopDrag() {
      this.isDragging = false
      document.removeEventListener('mousemove', this.handleDrag)
      document.removeEventListener('touchmove', this.handleDrag)
    }
  }
}
</script>

<style>
.rating-container {
  width: 220px;
}
.rating-track {
  width: 200px;
  height: 10px;
  background: #ddd;
  position: relative;
  border-radius: 5px;
  cursor: pointer;
}
.rating-thumb {
  width: 20px;
  height: 20px;
  background: #42b983;
  border-radius: 50%;
  position: absolute;
  top: -5px;
  transform: translateX(-50%);
  cursor: grab;
}
.rating-value {
  text-align: center;
  margin-top: 10px;
  font-size: 18px;
}
</style>

使用range input实现

简单HTML5 range输入实现:

<template>
  <div class="range-rating">
    <input 
      type="range" 
      min="0" 
      max="5" 
      step="0.1" 
      v-model="rating"
      @input="updateRating"
      class="rating-slider">
    <span>{{ rating }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rating: 0
    }
  },
  methods: {
    updateRating() {
      this.$emit('rating-changed', parseFloat(this.rating))
    }
  }
}
</script>

<style>
.rating-slider {
  width: 200px;
  -webkit-appearance: none;
  height: 10px;
  background: #ddd;
  outline: none;
  opacity: 0.7;
  transition: opacity .2s;
}
.rating-slider:hover {
  opacity: 1;
}
.rating-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  background: #4CAF50;
  cursor: pointer;
  border-radius: 50%;
}
</style>

使用Vuetify组件

如果使用Vuetify框架:

<template>
  <v-slider
    v-model="rating"
    :max="5"
    :step="0.5"
    thumb-label
    ticks
  ></v-slider>
</template>

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

每种方法适用于不同场景,第三方库方案最快速但定制性有限,自定义组件灵活性最高但需要更多开发时间,range input方案最简单但样式受限,Vuetify等UI框架方案适合已使用该框架的项目。

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

相关文章

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Comp…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…