基于Element的进度条Loading

基于Element的进度条loading组件

前端在中后台项目开发中经常要使用到loading遮罩层在数据交互时展示,目前我使用element组件库的loading,发现不能自定义,loading的样式比较少,官方只提供修改loading的图标和文字。

但是我的需求是希望展示的是进度条一样的效果,那就不用loading组件,使用element的Progress进度条组件,自己写一个。

代码如下。组件的api可以自行添加

javascript 复制代码
<template>
    <div class="loadingModal" :style="{'height':fatherHeight+'px'}" v-if="progressLoading">
        <el-progress
            :style="{width:'20%',margin:'0 auto',marginTop:`${fatherHeight*0.3}px`}"
            :type="type"
            :width="70"
            :text-inside="true"
            :stroke-width="type?undefined:strokeWidth"
            :percentage="percentage"
            :color="colors"
            :show-text="showText"
            :status="percentage === 100?undefined:'success'"
        ></el-progress>
    </div>
</template>
<script>
/**
 * @file progress.vue
 * @author yahooJ
 * @creatdate 2020-08-19
 * @update 2020-08-20
 */
export default {
  data() {
    return {
      percentage: 0,
      timeStart: 0,
      fatherHeight: 0, // 父级元素高度
      colors: [
        { color: "#1989fa", percentage: 99 },
        { color: "#2BD176", percentage: 100 },
      ],
      progressLoading: false,
    };
  },
  props: {
    strokeWidth: {
      type: Number,
      default: () => {
        return this.type ? undefined : 24;
      },
    },
    showText: {
      type: Boolean,
      default: true,
    },
    // 进度条状态
    type: {
      type: String,
      default: () => {
        return undefined;
      },
    },
    // 显示状态
    loading: {
      type: Boolean,
      default: false,
    },
  },
  mounted() {
    // 获取父级元素的高度,使组件高度与父元素高度相同
    // 相对于父元素定位
    this.$el.parentNode.style.position = "relative";
  },
  computed: {},
  methods: {
    // 进度条开始
    start() {
      const that = this;
      that.fatherHeight = this.$el.parentNode.offsetHeight;
      that.$nextTick(() => {
        that.progressLoading = true;
        that.percentage = 0;
        that.timeStart = setInterval(() => {
          if (that.percentage < 95) {
            that.percentage += 5;
          }
        }, 100);
      });
    },
    // 进度条结束
    end() {
      const that = this;
      that.percentage = 100;
      clearInterval(this.timeStart);
      setTimeout(() => {
        that.progressLoading = false;
      }, 300);
    },
  },
  watch: {
    // 监听loading状态控制进度条显示
    loading(value, newValue) {
      const that = this;
      if (value) {
        that.start();
      } else {
        that.end();
      }
    },
  },
};
</script>
<style >
.loadingModal {
  height: var(height);
  width: 100%;
  background: rgba(255, 255, 255, 0.9);
  text-align: center;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999999;
}
</style>

使用方式:

在需要遮罩的元素内放入即可,接收loading参数控制是否展示,展示时会覆盖父级元素

相关推荐
疯狂打码的少年7 小时前
【软件工程】结构化设计:模块独立性与耦合内聚
java·开发语言·笔记·软件工程
鹿目8 小时前
使用 Vant CLI 搭建 UI 组件库:从 H5 到小程序跨平台
前端·javascript
默_笙8 小时前
😭 我花了两小时找了一个数据结构 bug,才把"迷你 Cursor"跑起来,绝望(bushi)
前端·javascript
程高兴8 小时前
PMSM基于在线转动惯量辩识的滑模负载转矩观测器MATLAB-SIMULINK仿真模型
开发语言·matlab
竹林8188 小时前
wagmi v2 监听合约事件踩坑记:从 `useContractEvent` 到 `watchContractEvent`,我重构了三版才搞定实时数据流
前端·javascript
kisloy8 小时前
【python零基础教程第24讲】代码规范与质量管控
开发语言·python
C++、Java和Python的菜鸟8 小时前
第4章 后端Web基础(基础知识)
java·开发语言
芷栀夏8 小时前
Java教育平台实战复盘:课程、考试与学习行为分析系统设计
java·开发语言·学习
从零开始的代码生活_8 小时前
C++ 多态详解:虚函数、动态绑定、抽象类与虚表原理
开发语言·c++·后端·学习·算法
xywww1689 小时前
AWS 账号权限怎么分:根用户和 IAM 用户区别及日常使用建议
大数据·开发语言·人工智能·python·gpt·云计算·aws