当前位置:首页 > VUE

vue滑动评分实现

2026-03-08 23:04:19VUE

Vue 滑动评分实现方法

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

安装依赖:

npm install vue-rate --save

在组件中使用:

<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>

自定义滑动评分组件

创建可拖动滑块组件:

<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框架:

vue滑动评分实现

<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的结合 在Vue中实现多线程通常通过Web Worker技术完成。Web Worker允许在后台线程中运行脚本,避免阻塞主线程,适合处理计算密集型任务。 使用Web Worker的…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue登录实现

vue登录实现

Vue 登录实现 实现登录功能通常需要前端与后端配合,Vue 作为前端框架,主要负责表单处理、请求发送和状态管理。以下是常见的实现方式: 表单设计与数据绑定 使用 Vue 的 v-model 实现表…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…