在页面加载完成之前或者执行某操作时,先显示要显示的加载动画。
实现原理
先定义一个加载动画元素(最好是纯CSS实现,图片的话可能慢一点),当页面未加载完成之前,先使其“可见”,当页面加载完成后,再使其“不可见”。重要的一点就是怎样知道页面或者元素加载完成了,详情可以看一下:
https://blog.csdn.net/weixin_43670802/article/details/105875167
具体实现
加载动画页面HTML
<div id="loading">
<div id="loading_bg">
<div class="loader">Loading...</div>
</div>
</div>
加载动画页面CSS
/*********LoadingPage*************/
#loading {
position: absolute;
background-color: #FFF;:
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 9999;
}
#loading_bg{
background-color: rgba(0,0,0,0.7);
}
//body {
// background: #eaecfa;
//}
.loader {
width: 250px;
height: 50px;
line-height: 50px;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
font-family: helvetica, arial, sans-serif;
text-transform: uppercase;
font-weight: 900;
color: #ce4233;
letter-spacing: 0.2em;
}
.loader::before, .loader::after {
content: "";
display: block;
width: 15px;
height: 15px;
background: #ce4233;
position: absolute;
-webkit-animation: load .7s infinite alternate ease-in-out;
animation: load .7s infinite alternate ease-in-out;
}
.loader::before {
top: 0;
}
.loader::after {
bottom: 0;
}
@-webkit-keyframes load {
0% {
left: 0;
height: 30px;
width: 15px;
}
50% {
height: 8px;
width: 40px;
}
100% {
left: 235px;
height: 30px;
width: 15px;
}
}
@keyframes load {
0% {
left: 0;
height: 30px;
width: 15px;
}
50% {
height: 8px;
width: 40px;
}
100% {
left: 235px;
height: 30px;
width: 15px;
}
}
/********************************/
JS操作
//Loading页面
document.onreadystatechange=function () {
if (document.readyState=="complete"){
loadingFade();
}
}
function loadingFade() {
var opacity=1;
//var loadingPage=document.getElementById('loading');
var loadingBackground=document.getElementById('loading_bg');
var time=setInterval(function () {
if (opacity<=0){
clearInterval(time);
//loadingPage.remove();
$('#loading').remove();
}
loadingBackground.style.opacity=opacity;
opacity-=0.4;
},100);
}
参考
https://blog.csdn.net/qq_39036844/article/details/82454349?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2
https://www.runoob.com/jsref/prop-doc-readystate.htmlhttps://developer.mozilla.org/zh-CN/docs/Web/API/Document/readyState
https://www.cnblogs.com/sunny-sl/p/7977898.html
https://www.cnblogs.com/passlogs/p/6844065.html
https://blog.csdn.net/xiaokui_wingfly/article/details/51502209?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2