Vue3 项目开发技巧01-项目动画
开屏加载动画
效果预览
此演示仅在桌面端浏览器中可用
实现方式 1
主要依赖于定位、动画。
需要在 main.js
中添加如下代码:
js
// 移除开屏加载动画
const removeLoading = () => {
const element = document.getElementById("loading-box");
element?.remove();
};
在 gin-vue-admin 项目中,是在 permission.js 中去调用 removeLoading
函数。
html
<!DOCTYPE html>
<html lang="zh-cn">
<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" />
<link rel="icon" href="/favicon.ico" />
<title></title>
<style>
body {
margin: 0;
--bg-color: #409eff;
}
#loading-box {
position: relative;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
}
#loading-text {
position: absolute;
bottom: calc(50% - 100px);
left: 0;
width: 100%;
text-align: center;
color: #666;
font-size: 14px;
}
#loading {
position: absolute;
top: calc(50% - 20px);
left: calc(50% - 20px);
}
#box {
width: 50px;
height: 50px;
background: var(--bg-color);
animation: animate 0.5s linear infinite;
position: absolute;
top: 0;
left: 0;
border-radius: 3px;
}
@keyframes animate {
17% {
border-bottom-right-radius: 3px;
}
25% {
transform: translateY(9px) rotate(22.5deg);
}
50% {
transform: translateY(18px) scale(1, 0.9) rotate(45deg);
border-bottom-right-radius: 40px;
}
75% {
transform: translateY(9px) rotate(67.5deg);
}
100% {
transform: translateY(0) rotate(90deg);
}
}
#shadow {
width: 50px;
height: 5px;
background: #000;
opacity: 0.1;
position: absolute;
top: 59px;
left: 0;
border-radius: 50%;
animation: shadow 0.5s linear infinite;
}
.dark #shadow {
background: #fff;
}
@keyframes shadow {
50% {
transform: scale(1.2, 1);
}
}
</style>
</head>
<body>
<div id="loading-box">
<div id="loading">
<div id="shadow"></div>
<div id="box"></div>
</div>
<div id="loading-text">系统正在加载中...</div>
</div>
<div id="app"></div>
<script type="module" src="./src/main.js"></script>
</body>
</html>
实现方式 2
主要依赖于伪元素、动画。
优点:无需手动移除加载动画,当页面加载完成后,自动移除加载动画。
html
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>wanglei-shuaige~</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f6fafd;
}
.loader-body {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.loader {
width: 48px;
height: 48px;
margin: auto;
position: relative;
}
.loader:before {
content: "";
width: 48px;
height: 5px;
background: #f0808050;
position: absolute;
top: 60px;
left: 0;
border-radius: 50%;
animation: shadow324 0.5s linear infinite;
}
.loader:after {
content: "";
width: 100%;
height: 100%;
background: #f08080;
position: absolute;
top: 0;
left: 0;
border-radius: 4px;
animation: jump7456 0.5s linear infinite;
}
@keyframes jump7456 {
15% {
border-bottom-right-radius: 3px;
}
25% {
transform: translateY(9px) rotate(22.5deg);
}
50% {
transform: translateY(18px) scale(1, 0.9) rotate(45deg);
border-bottom-right-radius: 40px;
}
75% {
transform: translateY(9px) rotate(67.5deg);
}
100% {
transform: translateY(0) rotate(90deg);
}
}
@keyframes shadow324 {
0%,
100% {
transform: scale(1, 1);
}
50% {
transform: scale(1.2, 1);
}
}
</style>
</head>
<body>
<div id="app">
<div class="loader-body">
<div class="loader"></div>
</div>
</div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
全局过渡效果
同样的在 /index.html 中进行改造。
- 在根节点
html
上添加一个自定义类名。
html
<html lang="zh-cn" class="transition-colors"></html>
- 在
<style>
中添加全局过渡效果。
css
.transition-colors {
/*
指定了哪些CSS属性会有过渡效果
- 文字颜色(color)
- 背景色(background-color)
- 边框颜色(border-color)
- 文字装饰颜色(text-decoration-color)
- SVG填充色(fill)
- SVG描边色(stroke)
*/
transition-property: color, background-color, border-color,
text-decoration-color, fill, stroke;
/* 定义了过渡的时间曲线 */
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
/* 定义了过渡的时间 */
transition-duration: 150ms;
}