解决项目加载时空白页面

背景:当前端项目加载时,遇到网络不稳定或更新项目时,出现长时间白屏情况,对用户体验非常不友好。

解决方法

  • CSN加速
  • 增加带宽
  • 前端页面修改

本文就第三点展开

index.html页面 (public文件夹下)

html 复制代码
<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <link rel="stylesheet" href="<%= BASE_URL %>styles/base.css">
    <title>
        <%= htmlWebpackPlugin.options.title %>
    </title>
    <link href="./styles/base.css">
</head>

<body>
<div id="mainLoading">
    <div class="ddr ddr1"></div>
    <div class="ddr ddr2"></div>
    <div class="ddr ddr3"></div>
    <div class="ddr ddr4"></div>
    <div class="ddr ddr5"></div>
</div>
<div id="app" style="display: none"></div>
</body>

base.css

css 复制代码
#mainLoading {
    width: 100%;
    height: 80vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

#mainLoading .box {
    display: flex;
    align-items: center;
    justify-content: center;
}

#mainLoading .ddr {
    width: 8px;
    height: 100px;
    float: left;
    margin: 2px;
    background-color: #4890FF;
    animation: loading 1s infinite ease-in-out; /*animation:动画名称 持续时间 动画速度曲线 延迟 执行多少次 是否正反方向轮流播放*/
}

#mainLoading .ddr2 {
    animation-delay: -0.9s; /*定义开始执行的地方,负号表示直接从第900ms开始执行*/
}

#mainLoading .ddr3 {
    animation-delay: -0.8s;
}

#mainLoading .ddr4 {
    animation-delay: -0.7s;
}

#mainLoading .ddr5 {
    animation-delay: -0.6s;
}

@keyframes loading {
    0%,
    40%,
    100% {
        /*定义每帧的动作*/
        -webkit-transform: scaleY(0.5);
    }
    20% {
        -webkit-transform: scaleY(1);
    }
}

App.vue 添加钩子函数

html 复制代码
  mounted() {
    // 关闭loading
    document.getElementById('mainLoading').style.display = 'none';
    document.getElementById('app').style.display = 'block';
  },

效果图 loading动画覆盖白屏。

相关推荐
用户83134859306984 小时前
Cesium自定义可调节星空夜景+星星闪烁
vue.js·cesium
WindfallSheng6 小时前
从Vibe Coding到Spec Coding:一套可落地的AI-SDD企业级研发实战工程
javascript·vue.js
我有满天星辰6 小时前
Vue 指令完全指南:从 Vue 2 到 Vue 3 的演进与实战
前端·javascript·vue.js
我有满天星辰9 小时前
Vue 3 响应式双雄:ref 与 reactive 完全指南
前端·javascript·vue.js
leoZ23120 小时前
Claude 驱动的全栈开发:Spring Boot + Vue 的 BS 架构实践
vue.js·spring boot·架构
荒诞英雄1 天前
从 17MB Vendor 大包到按需加载:Vite 多入口 Vue 组件库首屏优化实践
vue.js·vite
jsonbro1 天前
手把手带你实现发布订阅模式
前端·vue.js·设计模式
Cobyte1 天前
23.Vue Vapor 组件 attrs 的实现
前端·javascript·vue.js
斯特凡今天也很帅1 天前
xml页面可以打开,不报错,但是显示数据的地方也不显示,我是怎么解决的
xml·vue.js
柯克七七1 天前
给老项目加了 TypeScript,本来只想自己爽,结果全公司代码审查标准被我抬高了
前端·vue.js·typescript