写一个简单的vue loading插件

写一个简单的vue loading插件。集成message功能:

 

目录:

plugins

  ---loading

     --loading.vue   //插件的样式和基本方法

    ---index.js        //插件的逻辑

    ---type.js          //用来区分 loading,warning,error,success等类型的一个render函数

----------------------------------------------------------------------------------------------------------------

loading.vue

<template>

  <transition name="slide">

    <div

      v-if="show"

      class="sloading loading-box fx p16 boxSize w"

    >

      <div class="icon-box">

        <Icon

          :type="type"

          :text="text"

        ></Icon>

      </div>

      <div class="cont-box fs12 f333">{{ text }}</div>

    </div>

  </transition>

</template>

<script>

import Icon from "./type.js";

export default {

  components: { Icon },

  data() {

    return {

      icon: require("@/assets/logo.png"),

      show: false,

      type: "loading",

      text: "数据加载中请稍后",

      progressColor: "#ff0000",

      color: "#ffa800",

    };

  },

};

</script>

<style>

.loading-box {

  min-width: 260px;

  position: fixed;

  top: 50%;

  left: 50%;

  transform: translate(-50%, -50%);

  box-shadow: 0 0 10px 1px #ccc;

  z-index: 2020;

}

.icon-box {

  flex: 0 1 70px;

  text-align: center;

  font-size: 18px;

}

.cont-box {

  flex: 1;

}

.rotate {

  animation: ani 1s ease-in infinite;

}

@keyframes ani {

  0% {

    -webkit-transform: rotate(0deg);

  }

  100% {

    -webkit-transform: rotate(360deg);

  }

}

.slide-enter-active,

.slide-leave-active {

  transition: all 0.5s;

  top: 50%;

  opacity: 1;

}

.slide-enter,

.slide-leave-active {

  opacity: 0;

}

.icon-loading {

  font-weight: bold;

}

.default {

  color: #ffa800;

}

.error {

  color: #f00;

}

.warning {

  color: #ffa800;

}

.success {

  color: #3ac24b;

}

</style>

----------------------------------------------------------------------------------------------------------------

index.js

 

import myLoading from "./loading.vue";

export default {

  install(Vue, options) {

    const Loading = Vue.extend(myLoading);

    const Profile = new Loading({

      el: document.createElement("div"),

    });

    document.body.appendChild(Profile.$el);

    if (options) {

      if (options.txt) {

        Profile.icon = options.txt;

      }

      if (options.color) {

        Profile.color = options.color;

      }

      if (options.type) {

        Profile.type = options.type;

      }

      options.mask = options.mask ? options.mask : false

    }

    const myLoadingMethod = {

      open(

        opts = { type: "loading", text: "加载中请稍后", duration: 2000, mask: true }

      ) {

        console.log("open:", opts.duration);

        let loadEle = document.querySelector(".sloading");

        Profile.show = true;

        console.log("Profile.show:", Profile.show);

        if (opts.text) {

          Profile.text = opts.text;

        }

        // if (opts.color) {

        //   Profile.color = opts.color;

        // }

        if (opts.type) {

          Profile.type = opts.type;

        }

        if (opts.duration) {

          Profile.duration = opts.duration;

          if (opts.duration !== 0) {

            setTimeout(() => {

              Profile.show = false;

            }, opts.duration);

          }

        }

        if (!opts.duration) {

          setTimeout(() => {

            Profile.show = false;

          }, 2000);

        }

 

      },

      close() {

        Profile.show = false;

      },

    };

    Vue.prototype.$sLoading = myLoadingMethod;

  },

};

----------------------------------------------------------------------------------------------------------------

type.js

export default {

    render(h) {

        return h("i", {

            attrs: {

                class: 'iconfont inBlock center'

            },

            'class': {

                'iconfont icon-loading rotate inBlock default': this.type == "loading",

                'iconfont icon-warning inBlock warning': this.type == "warning",

                'iconfont icon-error inBlock error': this.type == "error",

                'iconfont icon-success inBlock success': this.type == "success",

            }

        })

    },

    props: {

        type: {

            type: String,

            default: 'loading'

        },

        text: {

            type: String,

            default: '数据加载中请稍后'

        }

    }

}





 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值