当前位置:首页 > VUE

vue实现进度条

2026-02-21 19:02:02VUE

Vue 实现进度条的几种方法

使用原生 HTML5 <progress> 标签

Vue 可以结合 HTML5 的 <progress> 标签快速实现基础进度条:

<template>
  <progress :value="progressValue" max="100"></progress>
</template>

<script>
export default {
  data() {
    return {
      progressValue: 30 // 当前进度值
    }
  }
}
</script>

通过绑定 valuemax 属性控制进度显示,适合简单场景。

自定义 CSS 进度条

通过动态样式绑定实现更灵活的视觉效果:

<template>
  <div class="progress-container">
    <div 
      class="progress-bar" 
      :style="{ width: progressValue + '%' }"
    ></div>
  </div>
</template>

<style>
.progress-container {
  width: 100%;
  height: 20px;
  background-color: #f0f0f0;
  border-radius: 10px;
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  background-color: #42b983;
  transition: width 0.3s ease;
}
</style>

这种方法允许完全自定义进度条的颜色、高度和动画效果。

vue实现进度条

使用第三方库(如 Element UI)

对于需要丰富功能的场景,可以使用 UI 框架的进度条组件:

<template>
  <el-progress 
    :percentage="progressValue"
    :color="customColors"
    :stroke-width="20"
  ></el-progress>
</template>

<script>
export default {
  data() {
    return {
      progressValue: 65,
      customColors: [
        { color: '#f56c6c', percentage: 20 },
        { color: '#e6a23c', percentage: 40 },
        { color: '#5cb87a', percentage: 60 },
        { color: '#1989fa', percentage: 80 },
        { color: '#6f7ad3', percentage: 100 }
      ]
    }
  }
}
</script>

Element UI 提供多种预设样式和配置选项,如渐变色、环形进度条等。

动态加载进度条

结合异步操作实现真实场景的进度模拟:

vue实现进度条

<template>
  <div>
    <button @click="startLoading">开始加载</button>
    <progress :value="progress" max="100"></progress>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progress: 0,
      interval: null
    }
  },
  methods: {
    startLoading() {
      this.interval = setInterval(() => {
        this.progress += 10
        if (this.progress >= 100) clearInterval(this.interval)
      }, 500)
    }
  },
  beforeDestroy() {
    clearInterval(this.interval)
  }
}
</script>

通过定时器模拟数据加载过程,适合文件上传等场景。

环形进度条实现

使用 SVG 实现环形进度效果:

<template>
  <div class="circle-progress">
    <svg viewBox="0 0 100 100">
      <circle 
        cx="50" 
        cy="50" 
        r="45" 
        fill="none" 
        stroke="#eee" 
        stroke-width="8"
      />
      <circle
        cx="50"
        cy="50"
        r="45"
        fill="none"
        stroke="#42b983"
        stroke-width="8"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="offset"
      />
    </svg>
    <span>{{ progressValue }}%</span>
  </div>
</template>

<script>
export default {
  props: {
    progressValue: {
      type: Number,
      default: 0
    }
  },
  computed: {
    circumference() {
      return 2 * Math.PI * 45
    },
    offset() {
      return this.circumference * (1 - this.progressValue / 100)
    }
  }
}
</script>

<style>
.circle-progress {
  position: relative;
  width: 100px;
  height: 100px;
}
.circle-progress span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>

通过计算 SVG 圆的 stroke-dashoffset 属性实现环形填充效果。

关键注意事项

  • 进度值应该始终在 0 到 max 值之间,需要添加验证逻辑
  • 动态进度建议使用 CSS transition 实现平滑动画
  • 环形进度条需要正确计算圆的周长和偏移量
  • 销毁组件前清除定时器避免内存泄漏

标签: 进度条vue
分享给朋友:

相关文章

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &l…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…

vue底部实现

vue底部实现

Vue 底部实现方法 在 Vue 项目中实现底部布局可以通过多种方式完成,以下是一些常见的方法: 使用固定定位 将底部元素固定在页面底部,适用于单页应用或需要始终显示的底部栏。 <temp…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…