vue.js页面加载数据时加载动画过渡效果

本文详细介绍如何在Vue项目中创建和使用加载组件,包括.vue文件的结构、样式设置、过渡效果的实现,以及如何通过数据请求控制组件显示与隐藏。

创建组件

  • 新建 .vue 文件: src/components/laoding/laoding.vue
<template>
    <div class="loading"></div>
</template>
<script>

export default {
    name: 'Loading' // 定义的组件名称 使用时写法:loading
}

</script>
<style scoped>

.loading {
    position: fixed;
    left: 0;
    top: 0;
    background: url('~@/assets/images/loading-ball.svg') center center no-repeat #fff;
    width: 100vw;
    height: 100vh;
    z-index: 1000;
}

</style>

 

使用组件

  • 原理:

通过自定义一个变量 isLoading 初始化为 true ,在数据请求成功之后将变量改为 false ,在 template 中通过变量来控制是否显示隐藏,使用 vue 自带的 过渡效果 增加用户体验

  • 需要在全局的 css 中加入过渡需要的样式
.fade-enter,
    .fade-leave-active {
    opacity: 0;
}

.fade-enter-active,
.fade-leave-active {
    transition: opacity 0.5s;
}
  • .vue 文件使用代码示例片段
<template>
    <div>
        <transition name="fade">
            <loading v-if="isLoading"></loading>
        </transition>
    </div>
</template>
<script>

import Loading from '@/components/loading'
export default{
    components:{ Loading },

    data(){
       return{
           isLoading: true
       }
    },

    mounted() {
        const that = this

        // 初始化页面数据
        that.loadPageData()
    },

    methosd:{
        loadPageData: function() {
        // axios 请求页面数据 .then 中将状态值修改 this.isLoading = false
        },
    }
}

<script>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

船长在船上

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值