Skip to content

Progress 进度条 - Vant 4

📈 Progress 进度条

🎯 介绍

Progress 组件用于展示操作的当前进度,支持设置颜色、线条粗细、状态置灰等,直观体现任务完成度,就像生活中的进度条一样清晰明了 ⏳

📦 引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

js
import { createApp } from 'vue';
import { Progress } from 'vant';

const app = createApp();
app.use(Progress);

🚀 代码演示

🔧 基础用法

进度条默认为蓝色,使用 percentage 属性来设置当前进度,简单直观地展示完成状态 ✨

html
<van-progress :percentage="50" />
js
import { ref } from 'vue';

export default {
  setup() {
    // 模拟进度变化,动态展示进度效果 📊
    const percentage = ref(50);
    
    // 增加进度的函数
    const increaseProgress = () => {
      if (percentage.value < 100) {
        percentage.value += 10;
      }
    };
    
    // 重置进度的函数
    const resetProgress = () => {
      percentage.value = 0;
    };

    return {
      percentage,
      increaseProgress,
      resetProgress,
    };
  },
};

📏 线条粗细

通过 stroke-width 可以设置进度条的粗细,打造不同的视觉层次感 🎨

html
<van-cell-group>
  <van-cell title="细线条">
    <van-progress :percentage="50" stroke-width="2" />
  </van-cell>
  <van-cell title="标准线条">
    <van-progress :percentage="50" />
  </van-cell>
  <van-cell title="粗线条">
    <van-progress :percentage="50" stroke-width="8" />
  </van-cell>
</van-cell-group>
js
import { ref } from 'vue';

export default {
  setup() {
    const percentage = ref(50);

    return {
      percentage,
    };
  },
};

🌫️ 置灰

设置 inactive 属性后进度条将置灰,表示禁用或暂停状态 ⏸️

html
<van-cell-group>
  <van-cell title="正常状态">
    <van-progress :percentage="50" />
  </van-cell>
  <van-cell title="置灰状态">
    <van-progress :percentage="50" inactive />
  </van-cell>
</van-cell-group>
js
import { ref } from 'vue';

export default {
  setup() {
    const percentage = ref(50);
    const isInactive = ref(false);
    
    // 切换置灰状态
    const toggleInactive = () => {
      isInactive.value = !isInactive.value;
    };

    return {
      percentage,
      isInactive,
      toggleInactive,
    };
  },
};

🎨 样式定制

可以使用 pivot-text 属性自定义文字,color 属性自定义进度条颜色,打造个性化的进度展示 🌈

html
<van-cell-group>
  <van-cell title="自定义颜色">
    <van-progress :percentage="25" color="#f2826a" />
  </van-cell>
  <van-cell title="自定义文字">
    <van-progress :percentage="50" pivot-text="进行中" />
  </van-cell>
  <van-cell title="渐变色">
    <van-progress 
      :percentage="75" 
      color="linear-gradient(to right, #be99ff, #7232dd)" 
    />
  </van-cell>
  <van-cell title="隐藏文字">
    <van-progress :percentage="50" :show-pivot="false" />
  </van-cell>
</van-cell-group>
js
import { ref } from 'vue';

export default {
  setup() {
    const percentage = ref(50);
    const customColor = ref('#f2826a');
    const customText = ref('进行中');
    
    // 动态改变进度和样式
    const changeStyle = () => {
      percentage.value = Math.floor(Math.random() * 100);
      const colors = ['#f2826a', '#07c160', '#1989fa', '#ff976a'];
      customColor.value = colors[Math.floor(Math.random() * colors.length)];
    };

    return {
      percentage,
      customColor,
      customText,
      changeStyle,
    };
  },
};

🎯 动态进度

展示动态变化的进度条效果,模拟真实的加载场景 🔄

html
<van-cell-group>
  <van-cell title="文件上传">
    <van-progress :percentage="uploadProgress" color="#07c160" />
  </van-cell>
  <van-cell title="下载进度">
    <van-progress :percentage="downloadProgress" color="#1989fa" />
  </van-cell>
  <van-button @click="startUpload" type="primary" size="small">
    开始上传
  </van-button>
  <van-button @click="startDownload" type="success" size="small">
    开始下载
  </van-button>
</van-cell-group>
js
import { ref } from 'vue';

export default {
  setup() {
    const uploadProgress = ref(0);
    const downloadProgress = ref(0);
    
    // 模拟上传进度
    const startUpload = () => {
      uploadProgress.value = 0;
      const timer = setInterval(() => {
        uploadProgress.value += Math.random() * 10;
        if (uploadProgress.value >= 100) {
          uploadProgress.value = 100;
          clearInterval(timer);
        }
      }, 200);
    };
    
    // 模拟下载进度
    const startDownload = () => {
      downloadProgress.value = 0;
      const timer = setInterval(() => {
        downloadProgress.value += Math.random() * 15;
        if (downloadProgress.value >= 100) {
          downloadProgress.value = 100;
          clearInterval(timer);
        }
      }, 300);
    };

    return {
      uploadProgress,
      downloadProgress,
      startUpload,
      startDownload,
    };
  },
};

样式定制

可以使用 pivot-text 属性自定义文字,color 属性自定义进度条颜色。

html

📋 API

🎛️ Props

参数说明类型默认值
percentage进度百分比,控制进度条的完成度 📊number | string0
stroke-width进度条粗细,默认单位为 px,调节视觉厚度 📏number | string4px
color进度条颜色,支持渐变色,打造个性化外观 🎨string#1989fa
track-color轨道颜色,进度条背景色,营造对比效果 🛤️string#e5e5e5
pivot-text进度文字内容,自定义显示文本 📝string百分比
pivot-color进度文字背景色,文字区域的背景颜色 🏷️string同进度条颜色
text-color进度文字颜色,确保文字清晰可读 ✍️stringwhite
inactive是否置灰,表示禁用或暂停状态 🌫️booleanfalse
show-pivot是否显示进度文字,控制文字显示 👁️booleantrue

🏷️ 类型定义

组件导出以下类型定义:

ts
import type { 
  ProgressProps, 
  ProgressInstance,
  ProgressThemeVars 
} from 'vant';

ProgressInstance 是组件实例的类型,用法如下:

ts
import { ref } from 'vue';
import type { ProgressInstance } from 'vant';

const progressRef = ref<ProgressInstance>();

🎨 主题定制

🎭 样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值描述
--van-progress-height4px进度条高度,控制进度条的视觉厚度 📏
--van-progress-colorvar(--van-primary-color)进度条颜色,主要的进度显示色彩 🎨
--van-progress-inactive-colorvar(--van-gray-5)置灰状态颜色,禁用时的显示色彩 🌫️
--van-progress-backgroundvar(--van-gray-3)进度条背景色,轨道的基础颜色 🛤️
--van-progress-pivot-padding0 5px进度文字内边距,文字区域的留白空间 📦
--van-progress-pivot-text-colorvar(--van-white)进度文字颜色,确保文字清晰可读 ✍️
--van-progress-pivot-font-sizevar(--van-font-size-xs)进度文字字号,控制文字大小 📝
--van-progress-pivot-line-height1.6进度文字行高,优化文字垂直对齐 📐
--van-progress-pivot-backgroundvar(--van-primary-color)进度文字背景色,文字区域的背景颜色 🏷️

📚 相关文档

🔗 相关组件

基於Vant構建的企業級移動端解決方案