vue常用组件之loading

有的页面请求数据或者第一次加载的话会需要一定时间,这时候为了避免用户等待枯燥,可以适当加些loading之类的动态效果,下面是vue的一个动态loading组件,效果如下图:

代码如下:

<!--
 *
 * loadingGif组件--"数据请求中"
 *
 * 使用方法:
 * <loading-gif :show-loading="showLoaddingGif"></loading-gif>
 *
 *  通过修改showLoaddingGif的true或者false 来控制loading的显示和隐藏!
 *
 -->
<template>
    <div class='uil-default-css' style='transform:scale(0.3);' v-show="showLoading">
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(0deg) translate(0,-60px);transform:rotate(0deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(30deg) translate(0,-60px);transform:rotate(30deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(60deg) translate(0,-60px);transform:rotate(60deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(90deg) translate(0,-60px);transform:rotate(90deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(120deg) translate(0,-60px);transform:rotate(120deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(150deg) translate(0,-60px);transform:rotate(150deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(180deg) translate(0,-60px);transform:rotate(180deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(210deg) translate(0,-60px);transform:rotate(210deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(240deg) translate(0,-60px);transform:rotate(240deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(270deg) translate(0,-60px);transform:rotate(270deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(300deg) translate(0,-60px);transform:rotate(300deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
        <div style='top:80px;left:93px;width:14px;height:40px;background:#959199;-webkit-transform:rotate(330deg) translate(0,-60px);transform:rotate(330deg) translate(0,-60px);border-radius:10px;position:absolute;'></div>
    </div>
</template>

<script>
    export default {
        props: ["showLoading"]
    }
</script>

<style lang="sass" rel="stylesheet/scss">
    @-webkit-keyframes uil-default-anim {
        0% {
            opacity: 1
        }
        100% {
            opacity: 0
        }
    }

    @keyframes uil-default-anim {
        0% {
            opacity: 1
        }
        100% {
            opacity: 0
        }
    }

    .uil-default-css > div:nth-of-type(1) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: -0.5s;
        animation-delay: -0.5s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(2) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: -0.4166666666666667s;
        animation-delay: -0.4166666666666667s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(3) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: -0.33333333333333337s;
        animation-delay: -0.33333333333333337s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(4) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: -0.25s;
        animation-delay: -0.25s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(5) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: -0.16666666666666669s;
        animation-delay: -0.16666666666666669s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(6) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: -0.08333333333333331s;
        animation-delay: -0.08333333333333331s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(7) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: 0s;
        animation-delay: 0s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(8) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: 0.08333333333333337s;
        animation-delay: 0.08333333333333337s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(9) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: 0.16666666666666663s;
        animation-delay: 0.16666666666666663s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(10) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: 0.25s;
        animation-delay: 0.25s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(11) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: 0.33333333333333337s;
        animation-delay: 0.33333333333333337s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }

    .uil-default-css > div:nth-of-type(12) {
        -webkit-animation: uil-default-anim 1s linear infinite;
        animation: uil-default-anim 1s linear infinite;
        -webkit-animation-delay: 0.41666666666666663s;
        animation-delay: 0.41666666666666663s;
    }

    .uil-default-css {
        position: relative;
        background: none;
        width: 200px;
        height: 200px;
    }
</style>

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,可以通过在异步加载组件时添加loading状态来提供用户反馈。以下是一种常用的方式: 1. 创建一个Loading组件,用于显示加载状态的UI。例如,可以创建一个`Loading.vue`组件: ```vue <template> <div class="loading"> <span>Loading...</span> </div> </template> <script> export default { name: 'Loading', }; </script> <style scoped> .loading { display: flex; justify-content: center; align-items: center; height: 100px; } </style> ``` 2. 在异步加载组件时,可以添加一个loading标志位来表示加载状态。例如,在使用`import()`函数动态导入组件时: ```javascript data() { return { isLoading: true, // 初始加载状态为true component: null, // 初始化组件为null }; }, mounted() { import('./AsyncComponent.vue') .then((component) => { this.component = component.default; // 导入组件 this.isLoading = false; // 加载完成,loading状态为false }) .catch((error) => { console.error('Failed to load component:', error); this.isLoading = false; // 加载失败,loading状态为false }); }, ``` 3. 在模板中根据loading状态显示相应的内容。例如: ```vue <template> <div> <!-- 根据loading状态显示不同内容 --> <div v-if="isLoading"> <Loading /> </div> <div v-else> <component :is="component" /> </div> </div> </template> <script> import Loading from './Loading.vue'; export default { components: { Loading, }, // ... }; </script> ``` 在上述代码中,根据`isLoading`的值显示不同内容,当`isLoading`为`true`时,显示`Loading`组件;当`isLoading`为`false`时,显示异步加载的组件。 通过以上方式,您可以在异步加载组件时提供加载状态的反馈。希望能对您有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值