当前位置:网站首页>Project Performance Optimization Practice: solve the white screen problem of the home page, customize the loading animation to optimize the first screen effect
Project Performance Optimization Practice: solve the white screen problem of the home page, customize the loading animation to optimize the first screen effect
2022-07-19 02:23:00 【yulamz】
demand
When opening a page of the project for the first time , Because of the Internet and other reasons , It may cause the package volume to be too large .
Solution
preparation ( Use gka hold gif Turn it into svg)
Need help gka This tool . This tool is one click fast image optimization 、 Generating animation files . Just install globally , Cooperate with some commands , You can turn the picture into canvas/css/svg Animation .
First , Give Way UI hold .gif The motion picture is converted into a series of pictures , Each picture represents a single frame of this animation . And then by order gka < Folder path > -m true -t svg --minirate 0-20 -s, Merge the single frame of the animation into a sprite map , Compress according to a certain compression ratio , The last generation svg Format animation file .
Because the content of the article needs desensitization , Let's put a gka Figure in the document . The one on the left indicates loading gif All single frames of , use gka After the tool , It can be compressed into a sprite picture and matched .css file ,.html This file shows how to use this animation .
Generated files :
A single frame → svg / canvas Dynamic effect :
Used in projects svg Animation
First , Need to put gka Generated sprite map and css file , It's all in the middle of the project public Under the folder . This time I put sprite on public\img\loading-sprites.png,css The papers are in public\static\css\loading.css Next . Also on css The naming in the file has been optimized .
.loading-animation {
width: 640px;
height: 307px;
background-image: url("/img/loading-sprites.png");
background-repeat: no-repeat;
animation-name: keyframes-loading;
animation-duration: 2.88s;
animation-delay: 0s;
animation-iteration-count: infinite;
animation-fill-mode: forwards;
animation-timing-function: steps(1);
}
@keyframes keyframes-loading {
0% {
width: 640px;
height: 307px;
background-image: url("/img/loading-sprites.png");
}
1.39% {
background-position: -640px 0px;
}
2.78% {
background-position: -1280px 0px;
}
4.17% {
background-position: -1920px 0px;
}
5.56% {
background-position: -2560px 0px;
}
/* Some code is omitted here */
97.22% {
background-position: -44800px 0px;
}
98.61%, 100% {
background-position: -45440px 0px;
}
}
then , On the entry page index.html Introduce the above mentioned css file .
after , Reference resources gka Generated .html file , On the page body Add below svg Elements . Let the page open , It shows that loading The effect of , Replace the original white screen . Can pass svg Labeled style attribute , Appoint width Value , change svg Size .
<!DOCTYPE html>
<html lang="zh">
<head>
<!-- Other code is omitted here -->
<link rel="stylesheet" href="/static/css/loading.css">
</head>
<body>
<div id="first-loading-wrap" style="display: flex; justify-content: center; align-items: center; height:96vh; z-index: 999;">
<svg id="first-loading-svg" viewBox="0, 0, 640, 307" style="width: 320px">
<foreignObject id="first-loading-foreignobject" width="640" height="307">
<div id="first-loading-content" class="loading-animation"></div>
</foreignObject>
</svg>
</div>
<div id="app">
</div>
<!-- built files will be auto injected -->
</body>
</html>
Last , On the route afterEach In the hook function , remove loading Effect related elements can .
router.afterEach(to => {
// Determine whether it is necessary to remove loading effect
document.getElementById('first-loading-wrap')?.remove()
document.getElementById('first-loading-svg')?.remove()
document.getElementById('first-loading-foreignobject')?.remove()
document.getElementById('first-loading-content')?.remove()
// Omit other logic
})
边栏推荐
- 剑指 Offer 48. 最长不含重复字符的子字符串
- 第二讲 BTC-密码学原理(笔记)
- DQN理论基础及其代码实现【Pytorch + CartPole-v0】
- 元宇宙公链Caduceus项目详解(一):Caduceus Metaverse Protocol的项目理念及技术框架
- Gdb+vscode for debugging 1 -- compile and debug using cmakelist files + attach process debugging
- Lecture 2 BTC cryptography principles (notes)
- SSTI template injection
- 测试知识准备
- 第1章-多智能体系统
- [unity Editor Extension] unity publishes resources to the store process unity asset store publish
猜你喜欢
随机推荐
STL -- List container (linked list)
STL--map容器
Jmeter接口测试之响应断言
【AntV G2】如何解决 G2 造成的内存泄露
攻防世界----shrine
【Unity编辑器扩展】Unity内部Asset资源配置ScriptableObject
静态路由(详)
Bugku problem solution
How to configure multiple SSH keys for novices (easy to understand hand-in-hand teaching)
简单记录一下并查集
Vmware Tools最新安装教程(RHEL8)
【Antv G2】折线图如何添加点击事件(点击任意位置即可获取折线上点的值)
英文商务邮件常用语
【工具篇】Unity2D人物控制器,控制2D玩家移动跳跃,四方向和水平方向
Nmon使用方法
Jmeter响应时间测试组件&多接口并发
innodb、Mysql结构、三种删除的区别
SSTI模板注入
status 500 reading AftersaleService#getAftersaleList(Long)+com.sun.proxy.$Proxy214.getAftersaleList
去中心化边缘渲染元宇宙协议Caduceus受邀出席CBAIA 2022峰会,以技术赋能更多Web3应用场景









