weex-ui的wxc-loading组件扩展,支持自定义gif、尺寸等

weex-ui官网的wxc-loading加载提示组件,可能因为时间紧急吧,一些扩展性做的不够好,看了代码就清楚了比如这里把一些东西写死了:
image.png
image.png

当我们直接引入weex-ui并想使用wxc-loading组件时这些写死的地方往往会限制我们的使用,比如只能用它提供的gif图,如果产品经理要我们换图就歇菜了。这些也只有自己用到它的时候才会体会到吧...所以我在wxc-loading的基础上稍微扩展了一下,让gif图、遮罩层的尺寸、背景颜色、提示文字的颜色能够自定义传入,实现扩展。
把weex-ui的wxc-loading组件单独拎出来,做成一个vue文件,命名为wxc-loadingsp.vue,整个wxc-loadingsp.vue代码如下(简书好像不能传文件,这里直接贴完整组件代码,有需要的小伙伴直接拷贝就好):


<!-- Created by shayneChen on 2018/08/09. -->
<!--全局加载提示组件. -->
<template>
  <div :class="[showLoading && needMask && 'loading-need-mask']"
       @click="maskClicked"
       :style="maskStyle">
    <div class="wxc-loading" :style="[centerPosition]" v-if="showLoading">
      <div :class="['loading-box',mBackgroundcolor?'':'transparent']" :style="[conStyle]" :aria-hidden="true">
        <image :src="loading.url"
               :style="{width:mImgwidth,height:mImgheight}"
               resize="contain"
               quality="original"></image>
        <text v-if="loadingText" :style="{color:mTextcolor}" class="loading-text">{{loadingText}}</text>
      </div>
    </div>
  </div>
</template>

<style scoped>
  .loading-need-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.3);
  }
  .wxc-loading {
    position: fixed;
    z-index: 9999;
  }
  .loading-box {
    align-items: center;
    justify-content: center;
    border-radius: 20px;
  }
  .transparent {
    background-color: rgba(0, 0, 0, .2); 
  }
  .loading-trip-image {
    height: 166px;
    width: 199px;
  }
  .loading-text {
    font-size: 30px;
    line-height: 46px;
    height: 46px;
    margin-top: 8px;
    text-overflow: ellipsis;
    padding-left: 12px;
    padding-right:12px;    
    margin-bottom: 8px;
    text-align: center;
  }
</style>

<script>

  export default {
    props: {
      show: {
        type: Boolean,
        default: false
      },
      loadingText: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: 'default'
      },
      interval: {
        type: [Number, String],
        default: 0
      },
      needMask: {
        type: Boolean,
        default: false
      },
      maskStyle: {
        type: Object,
        default: () => ({})
      },
      mHeight: {
        type: String,
        default: '175'
      },
      mWidth: {
        type: String,
        default: '175'
      },
      mBackgroundcolor: {
        type:String,
        default: ''
      },
      mUrl: {
        type:String
      },
      mTextcolor: {
        type:String,
        default:'#ffffff'
      },
      mImgwidth: {
        type:String,
        default:'200'
      },
      mImgheight:{
        type:String,
        default:'200'
      },
      mTop: {
        type:String,
        default:''
      }
    },
    data: () => ({
      showLoading: false,
      tid: 0
    }),
    watch: {
      show () {
        this.setShow();
      }
    },
    computed: {
      //整个容器的尺寸和背景样式
      conStyle() {
        let style = {};
        style["width"] = `${this.mWidth}px`;
        style["height"] = `${this.mHeight}px`;
        if(this.mBackgroundcolor) {
          style["backgroundColor"] = this.mBackgroundcolor
        }
        return style;
      },
      //根据自定义的高宽来居中定位
      centerPosition() {
        let style = {};
        style["top"] = `${this.mTop ? this.mTop : this.topPosition}px`;
        style["left"] = `${(750-this.mWidth)/2}px`;
        return style;
      },
      topPosition () {
        return (this.getPageHeight() - this.mHeight) / 2;
      },
      loading () {
        let loading = {};
        switch (this.type) {
          case 'custom':
            loading = {
              url: this.mUrl
            };
            break;
          //不指定type为cunstom则用官网默认gif图
          default:
            loading = {
              url: 'https://img.alicdn.com/tfs/TB1Ep_9NVXXXXb8XVXXXXXXXXXX-74-74.gif'
            }
        }
        return loading;
      },
    },
    created () {
      this.setShow();
    },
    methods: {
     /**
     * 获取weex屏幕真实的设置高度,需要减去导航栏高度
     * @returns {Number}
     */
      getPageHeight () {
        const { env } = weex.config;
        // const navHeight = Utils.env.isWeb() ? 0 : (Utils.env.isIPhoneX() ? 176 : 132);
        return env.deviceHeight / env.deviceWidth * 750 - 132;
      },
      maskClicked () {
        this.needMask && (this.$emit('wxcLoadingMaskClicked', {}));
      },
      setShow () {
        const { interval, show, showLoading } = this;
        const stInterval = parseInt(interval);
        clearTimeout(this.tid);
        if (show) {
          if (showLoading) {
            return;
          }
          if (stInterval === 0) {
            this.showLoading = true;
          } else {
            this.tid = setTimeout(() => {
              this.showLoading = true;
            }, stInterval);
          }
        } else {
          this.showLoading = false;
        }
      }
    }
  };
</script>

放到文件夹后,import后这么使用

<!--此处m-打头的是我们自定义的特性-->
    <wxc-loadingsp 
      :show="showPrintLoading" 
      type="custom" 
      need-mask="true" 
      loading-text="名片打印中..." 
      m-url="./image/print.gif"     
      m-width="380" 
      m-height="380" 
      m-backgroundcolor="#ffffff" 
      m-textcolor="#313131"
      >
    </wxc-loadingsp>   
<!--此处省略-->
 import WxcLoadingsp from './components/wxc-loadingsp.vue';
<!--此处省略-->
  components: {
    WxcLoadingsp
  },
<!--此处省略-->

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,336评论 3 119
  • 这一周我很荣幸被班主任推选为小组组长,对一个久九营的新手来说,有点受宠若惊,也有点担忧,这份担忧我感受到了自己的不...
    九河嘛嘛阅读 320评论 2 1
  • “我们的经济很发达,可是却没有发展出经济学。”经济学是学问,学问就是要形成体系,体系就是建立金字塔结构。而金字塔结...
    ZXH_light阅读 505评论 0 0
  • 在这里,我唤着你的名字,你的名字却在山之外的风声里飘来飘去,如天空之城的幻影,我乘着风去,脚印是梦中的痕迹,却不小...
    马嘉喜欢深呼吸阅读 331评论 4 3
  • imagView.contentMode = UIViewContentModeScaleAspectFill; ...
    栋了个栋阅读 194评论 0 0

友情链接更多精彩内容