vue渐变色进度条。适用真假进度条

业务需要,动手封装一个渐变色进度条。组件适用真假进度条,
1、调用子组件的setProgress()方法,进度条开始执行,到91%进度条进入等待状态。待后台接收完数据,修改finished值为ture,进度条继续执行到100%。
2、如果需要真的进度条,调用接口轮循获取进度值progress传入组件。

在这里插入图片描述

<template>
  <hprogress ref="hjprogress" @isFinished="isFinished" :finished="finished" />
</template>
mounted(){
if (!this.finished) {
          //   this.setProgress();
          this.$nextTick(function () {
            this.$refs.hjprogress.setProgress();
          });
          setTimeout(() => { //模仿后台处理完毕返回
            this.finished = true;
          }, 10000);
        }
}
<template>
  <div class="mainConImport">
    <!-- <p>数据上传中</p> -->
    <div class="mainConImportProgress">
      <div class="mainConImportProgressBack">
        <span :style="{'width':progress+'%'}"></span>
      </div>
      <div class="mainConImportProgressPresent" v-if="progress<100">已上传{{progress}}%</div>
      <div class="mainConImportProgressPresent" v-else>上传成功</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      timer: null,
      progress: this.progres,
      initTime: this.initTimes,
    };
  },
  props: {
    progres: {
      type: Number,
      default: 0,
    },
    initTimes: { 
      type: Number,
      default: 70,//上传进度增长周期
    },
    finished: {
      type: Boolean,
      default: false,//是否确认完成进度条,为false则会在maxSlow值处暂停,直到后台返回结果,完成进度条,可根据finished值修改模拟上传速度
    },
    minSlow: {
      type: Number,
      default: 60,//模拟上传,完成60%后,且上传完成90%之前 开始降低上传速度
    },
    maxSlow: {
      type: Number,
      default: 90,//模拟上传,完成90%后暂停上传,等待后台返回结果。
    },
  },
  watch:{
    progress(val){
        if(val == 100){
            this.$emit('isFinished')
        }
    }
  },
  methods: {
    setProgress() {
      this.timer = setTimeout(() => {
        this.progress++;
        this.setProgress();
        if (this.progress > this.minSlow && this.progress < this.maxSlow) {
          if (!this.finished) {
              this.initTime += 4; 
          }else{
              //此处可重置上传进度增长周期
          }
        }
        if (this.progress > this.maxSlow) {
          if (!this.finished) {
            this.progress = this.maxSlow + 1;
          } else {
            if (this.progress > 99) {
              clearTimeout(this.timer);
              this.timer = null;//清除定时器,避免导致内存溢出
            }
          }
        }
      }, this.initTime);
    },
  },
};
</script>
<style lang="less" scoped>
.mainConImport {
  &Progress {
    height: 60px;
    margin: 60px 0;
    &Back {
      margin: 0 auto;
      height: 20px;
      max-width: 1000px;
      background-color: #ebeef5;
      border-radius: 12px;
      border-radius: 12px;
      padding: 0 15px;
      span {
        display: inline-block;
        height: 12px;
        background-image: linear-gradient(270deg, #33bea6 0%, #c8f1d5 100%);
        border-radius: 12px;
        border-radius: 12px;
      }
    }
    &Present {
      line-height: 16px;
      font-size: 16px;
      text-align: center;
      color: #333;
      margin-top: 22px;
    }
  }
}
</style>

青浅个人博客:http://www.qingqian.site:9527

要实现渐变色进度条,你可以使用 Vue.js 框架和 Element UI 组件库。具体实现如下: 1. 在 Vue 组件中使用 el-progress 组件,设置 type 为 circle 或 line,根据需求设置不同的属性。 2. 在组件中定义 CSS 样式,使用渐变色作为进度条的背景颜色。例如: ```css .el-progress__text { color: #000; } .el-progress__text { color: #000; } .el-progress-bar__outer { background: linear-gradient(to right, #f00, #ff0, #0f0); border-radius: 100px; } .el-progress-bar__inner { border-radius: 100px; } ``` 其中,linear-gradient() 函数用于设置渐变色,to right 表示从左到右渐变,#f00、#ff0、#0f0 是三种颜色,可以根据实际需求调整。 3. 在 Vue 组件中的 el-progress 组件中添加 slot 插槽,插入自定义的内容。例如: ```html <el-progress :percentage="percentage" :type="type"> <div class="progress-text">{{ percentage }}%</div> </el-progress> ``` 其中,percentage 是进度条的百分比,type 是进度条的类型,progress-text 是自定义的样式类名。 4. 在 Vue 组件中定义 data 数据,包括进度条的百分比和类型等。 完整的代码示例如下: ```html <template> <div> <el-progress :percentage="percentage" :type="type"> <div class="progress-text">{{ percentage }}%</div> </el-progress> </div> </template> <script> export default { data() { return { percentage: 50, type: 'circle', }; }, }; </script> <style> .el-progress__text { color: #000; } .el-progress-bar__outer { background: linear-gradient(to right, #f00, #ff0, #0f0); border-radius: 100px; } .el-progress-bar__inner { border-radius: 100px; } .progress-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 14px; font-weight: bold; } </style> ``` 通过以上步骤,你就可以实现一个具有渐变色进度条了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值