前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue 折腾记 - (15) 捣鼓一个中规中矩loading组件

Vue 折腾记 - (15) 捣鼓一个中规中矩loading组件

作者头像
CRPER
发布2019-04-09 17:33:43
8980
发布2019-04-09 17:33:43
举报
文章被收录于专栏:CRPER折腾记CRPER折腾记

前言

最近有一个新的项目,UI大佬不知道从哪里找来了一张GIF丢到蓝湖,

说作为全局的页面loading ,但是自己想了想,还是选择画一个。

一开始想过用svg,canvas;最终还是选择了css3+js来实现这个效果;

gif的缺点挺多,至于为什么又排除了svgcanvas

是因为css3+js可控性更强,不管是大小还是颜色,还是响应式(我的项目走的vh,vw)那套来适配;

可以借助打包插件,达到loading的大小适配;


效果

UI大佬提供的GIF

实现的效果【在线codesandbox预览】

  • 支持环的颜色改变
  • 支持在loading底部显示文字并控制其样式

实现思路

这个东东主要用了这么几个要点来实现完整的效果;

flexposition来布局

  • 伪类的颜色继承(currentColor)
  • 边框结合圆角实现环
  • 用了transformanimation来实现了整个过渡

效果知道怎么实现了,剩下的就是我们需要实现的功能点了;

因为是面向移动端的,所以这些常规的东东也要考虑下

  • 遮罩层可控
  • 防止点击穿透滚动body
  • 支持函数方法调用

源码

  • Loading.vue
代码语言:javascript
复制
<template>
  <div id="loading-wrapper">
    <div class="loading-ring" :style="ringStyle">
      <div class="outer"></div>
      <div class="middle"></div>
      <div class="inner"></div>
    </div>
    <div class="text" :style="textStyle" v-if="text">{{ text }}</div>
  </div>
</template>

<script>
export default {
  name: "Loading",
  props: {
    text: {
      type: String,
      default: ""
    },
    textStyle: {
      type: Object,
      default: function() {
        return {
          fontSize: "14px",
          color: "#fff"
        };
      }
    },
    ringStyle: {
      type: Object,
      default: function() {
        return {
          width: "100px",
          height: "100px",
          color: "#407af3"
        };
      }
    }
  },
  methods: {
    preventDefault() {
      // 禁止body的滚动
      document.querySelector("body").addEventListener("touchmove", function(e) {
        e.preventDefault();
        e.stopPropagation();
      });
    }
  },
  mounted() {
    this.preventDefault();
  },
  destroyed() {
    document
      .querySelector("body")
      .removeEventListener("touchmove", function(e) {
        e.preventDefault();
        e.stopPropagation();
      });
  }
};
</script>

<style lang="scss" scoped>
#loading-wrapper {
  position: fixed;
  left: 0;
  top: 0;
  height: 100vh;
  width: 100vw;
  background-color: rgba(0, 0, 0, 1);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  .loading-ring {
    position: relative;
    width: 200px;
    height: 200px;
    .outer,
    .inner,
    .middle {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      color: currentColor;
      &::after {
        content: "";
        display: block;
        width: 100%;
        height: 100%;
        border-radius: 100%;
        border-left: 10px solid currentColor;
        border-right: 10px solid currentColor;
        border-top: 10px solid currentColor;
        border-bottom: 10px solid transparent;
      }
    }

    .outer {
      width: 100%;
      height: 100%;
      &::after {
        animation: anticlockwise 1.5s infinite linear;
      }
    }
    .inner {
      width: calc(100% * 0.6);
      height: calc(100% * 0.6);
      &::after {
        animation: anticlockwise 1.5s infinite linear;
      }
    }
    .middle {
      width: calc(100% * 0.8);
      height: calc(100% * 0.8);
      &::after {
        animation: clockwise 1.5s infinite linear;
      }
    }
  }

  .text {
    color: #fff;
    font-size: 14px;
    padding: 20px;
  }
}

@keyframes clockwise {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes anticlockwise {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(-360deg);
  }
}
</style>

复制代码
  • index.js
代码语言:javascript
复制
import Loading from "./Loading.vue";
// 来保持实例,单例模式
let instance;
let el;

Loading.install = function(Vue, options = {}) {
  const defaultOptions = {
    text: "",
    textStyle: {
      fontSize: "14px",
      color: "#fff"
    },
    ringStyle: {
      width: "100px",
      height: "100px",
      color: "#407af3"
    },
    ...options
  };
  Vue.prototype.$loading = {
    show(options = {}) {
      if (!instance) {
        let LoadingInstance = Vue.extend(Loading);
        el = document.createElement("div");
        document.body.appendChild(el);
        instance = new LoadingInstance({
          propsData: { defaultOptions, ...options }
        }).$mount(el);
      } else {
        return instance;
      }
    },
    hide() {
      if (instance) {
        document.body.removeChild(document.getElementById("loading-wrapper"));
        instance = undefined;
      }
    }
  };
};

export default Loading;


复制代码

选项及用法

选项

代码语言:javascript
复制
    text: {  // 这个不为空就在loading下面显示文字
      type: String,
      default: ""
    },
    textStyle: {  // loading text 的样式,颜色及字体大小
      type: Object,
      default: function() {
        return {
          fontSize: "14px",
          color: "#fff"
        };
      }
    },
    ringStyle: {  // 最外环的大小,内二环是比例换算的(百分比)
      type: Object,
      default: function() {
        return {
          width: "100px",
          height: "100px",
          color: "#407af3"
        };
      }
    }

复制代码

用法

在主入口use一下便可全局使用

除了常规的引入使用,还支持函数调用,挂载了一个$loading

代码语言:javascript
复制
this.$loading.show({
        text: "loading",
        textStyle: {
          fontSize: "18px",
          color: "#f00"
        }
      });
      
let st = setTimeout(() => {
        clearTimeout(st);
        this.$loading.hide();
    }, 1000);

复制代码

总结

props的传递没有做增量合并(递归每个key赋值),直接浅复制过的

对于组件功能的概而全,拓展性,大小需要自己权衡;

到这里,我们业务需要的一个小组件,该有的功能都有了。

有不对之处请留言,会及时修正。。。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年04月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 效果
  • 实现思路
  • 源码
  • 选项及用法
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档