当前位置:首页 > VUE

vue实现进度条

2026-01-21 03:31:03VUE

Vue 实现进度条的方法

使用原生 HTML 和 CSS 结合 Vue 数据绑定

创建一个简单的进度条,通过 Vue 的数据绑定动态控制进度值。

<template>
  <div class="progress-container">
    <div class="progress-bar" :style="{ width: progress + '%' }"></div>
  </div>
  <button @click="increaseProgress">增加进度</button>
</template>

<script>
export default {
  data() {
    return {
      progress: 0
    }
  },
  methods: {
    increaseProgress() {
      if (this.progress < 100) {
        this.progress += 10
      }
    }
  }
}
</script>

<style>
.progress-container {
  width: 100%;
  height: 20px;
  background-color: #e0e0e0;
  border-radius: 10px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background-color: #42b983;
  transition: width 0.3s ease;
}
</style>

使用第三方组件库

Vue 生态中有许多现成的 UI 组件库提供了进度条组件,例如 Element UI、Vuetify 等。

vue实现进度条

以 Element UI 为例:

vue实现进度条

<template>
  <el-progress :percentage="progress"></el-progress>
  <el-button @click="increaseProgress">增加进度</el-button>
</template>

<script>
export default {
  data() {
    return {
      progress: 0
    }
  },
  methods: {
    increaseProgress() {
      if (this.progress < 100) {
        this.progress += 10
      }
    }
  }
}
</script>

实现环形进度条

通过 SVG 和 Vue 结合,可以创建环形进度条。

<template>
  <div class="circular-progress">
    <svg viewBox="0 0 36 36">
      <path
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
        fill="none"
        stroke="#e0e0e0"
        stroke-width="2"
      />
      <path
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
        fill="none"
        stroke="#42b983"
        stroke-width="2"
        :stroke-dasharray="`${progress}, 100`"
      />
    </svg>
    <span>{{ progress }}%</span>
  </div>
  <button @click="increaseProgress">增加进度</button>
</template>

<script>
export default {
  data() {
    return {
      progress: 0
    }
  },
  methods: {
    increaseProgress() {
      if (this.progress < 100) {
        this.progress += 10
      }
    }
  }
}
</script>

<style>
.circular-progress {
  position: relative;
  width: 100px;
  height: 100px;
}

.circular-progress svg {
  width: 100%;
  height: 100%;
}

.circular-progress span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
}
</style>

动画效果增强

为进度条添加平滑的动画效果,提升用户体验。

<template>
  <div class="progress-container">
    <div class="progress-bar" :style="{ width: animatedProgress + '%' }"></div>
  </div>
  <input type="range" v-model="progress" min="0" max="100">
</template>

<script>
export default {
  data() {
    return {
      progress: 0,
      animatedProgress: 0
    }
  },
  watch: {
    progress(newVal) {
      const animate = () => {
        if (this.animatedProgress < newVal) {
          this.animatedProgress += 1
          requestAnimationFrame(animate)
        } else if (this.animatedProgress > newVal) {
          this.animatedProgress -= 1
          requestAnimationFrame(animate)
        }
      }
      animate()
    }
  }
}
</script>

这些方法提供了从简单到复杂的进度条实现方案,可以根据项目需求选择合适的方式。

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

相关文章

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现爬虫

vue实现爬虫

Vue 实现爬虫的基本思路 Vue.js 本身是一个前端框架,主要用于构建用户界面。要实现爬虫功能,通常需要结合后端技术或浏览器自动化工具。以下是几种常见的方法: 方法一:Vue + Node.js…

vue实现分站

vue实现分站

Vue 实现分站的方案 在 Vue 中实现分站功能通常涉及路由配置、环境变量管理和动态加载资源。以下是几种常见的实现方式: 基于路由的分站实现 通过 Vue Router 配置多路由,区分不同站点的…

vue实现秒表

vue实现秒表

实现秒表功能 在Vue中实现秒表功能需要处理时间显示、开始、暂停和重置逻辑。以下是具体实现方式: 基础实现代码 <template> <div> <div&…