<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="body-container">
<div class="btn-container">
<button class="my-btn" id="btn" onclick="testLoading(1)">点击加载1</button>
<button class="my-btn" id="btn" onclick="testLoading(2)">点击加载2</button>
<button class="my-btn" id="btn" onclick="testLoading(3)">点击加载3</button>
</div>
<div class="loading-container"></div>
</div>
<script>
function Loading(config) {
this.type = config.type || 1;
this.tipLabel = config.tipLabel || "loading...";
this.wrap = config.wrap || document.body;
this.loadingWrapper = null;
}
Loading.prototype.init = function () {
this.createDom();
}
Loading.prototype.createDom = function () {
var loadingWrapper = document.createElement('div');
loadingWrapper.className = 'loading-wrapper';
var loadingView = document.createElement('div');
loadingView.className = 'loading-view';
var tipView = document.createElement('div');
tipView.className = 'tip-view';
tipView.innerText = this.tipLabel;
switch (this.type) {
case 1:
html = `
<div class="container1">
<div class="circle circle1"></div>
<div class="circle circle2"></div>
<div class="circle circle3"></div>
<div class="circle circle4"></div>
</div>
<div class="container2">
<div class="circle circle1"></div>
<div class="circle circle2"></div>
<div class="circle circle3"></div>
<div class="circle circle4"></div>
</div>
`;
loadingView.innerHTML = html;
break;
case 2:
var html = `
<div class="bounce-view">
<div class="bounce bounce1"></div>
<div class="bounce bounce2"></div>
<div class="bounce bounce3"></div>
</div>
`;
loadingView.innerHTML = html;
break;
case 3:
var html = `
<div class="wave">
<div class="react react1"></div>
<div class="react react2"></div>
<div class="react react3"></div>
<div class="react react4"></div>
<div class="react react5"></div>
</div>
`;
loadingView.innerHTML = html;
break;
default:
break;
}
loadingWrapper.appendChild(loadingView);
loadingWrapper.appendChild(tipView);
this.wrap.style.display = 'block';
this.wrap.appendChild(loadingWrapper);
this.loadingWrapper = loadingWrapper;
}
Loading.prototype.hide = function () {
this.wrap.removeChild(this.loadingWrapper);
this.wrap.style.display = 'none';
}
function testLoading(type) {
var loadingInstance = new Loading({
type,
tipLabel: '加载中...',
wrap: document.getElementsByClassName('loading-container')[0]
});
loadingInstance.init();
setTimeout(() => {
loadingInstance.hide();
}, 2000)
}
</script>
</body>
<style>
body {
background-color: grey;
margin: 0;
padding: 0;
}
.body-container{
width: 100%;
height: 100%;
}
.btn-container{
width: 100%;
height: 50px;
top:0;
}
.my-btn {
width: 100px;
height: 50px;
background-color: aqua;
}
.loading-container {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
position: absolute;
top:0;
z-index: 99999;
display: none;
}
.loading-wrapper {
width: 100px;
height: 100px;
background-color: rgba(0, 0, 0, 0.8);
border-radius: 10px;
color: #fff;
text-align: center;
display: flex;
flex-direction: column;
justify-content: space-evenly;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.loading-view {
height: 40px;
position: relative;
}
.container1,
.container2 {
width: 40px;
height: 40px;
position: absolute;
top: 0;
left: 50%;
margin-left: -20px;
position: absolute;
}
.container2 {
transform: rotate(45deg);
}
.circle,
.bounce {
width: 10px;
height: 10px;
background-color: #fff;
border-radius: 50%;
animation: loading 1.2s both infinite;
}
.circle {
position: absolute;
}
.circle1 {
top: 0;
left: 0;
}
.circle2 {
top: 0;
right: 0;
}
.circle3 {
bottom: 0;
right: 0;
}
.circle4 {
bottom: 0;
left: 0;
}
.container1 .circle1 {
animation-delay: 0s;
}
.container2 .circle1 {
animation-delay: 0.2s;
}
.container1 .circle2 {
animation-delay: 0.3s;
}
.container2 .circle2 {
animation-delay: 0.4s;
}
.container1 .circle3 {
animation-delay: 0.5s;
}
.container2 .circle3 {
animation-delay: 0.6s;
}
.container1 .circle4 {
animation-delay: 0.7s;
}
.container2 .circle5 {
animation-delay: 0.8s;
}
@keyframes loading {
0% {
transform: scale(0);
}
40% {
transform: scale(1);
}
80% {
transform: scale(0);
}
100% {
transform: scale(0);
}
}
.bounce-view {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.bounce {
display: inline-block;
}
.bounce1 {
animation-delay: -0.32s;
}
.bounce2 {
animation-delay: -0.16s;
}
.bounce3 {
animation-delay: 0s;
}
.wave {
width: 100px;
height: 40px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: flex;
justify-content: space-evenly;
}
.react {
width: 3px;
height: 40px;
background-color: #fff;
animation: waveLoading 1.2s both infinite;
}
.react1 {
animation-delay: 0s;
}
.react2 {
animation-delay: -1.1s;
}
.react3 {
animation-delay: -1.0s;
}
.react4 {
animation-delay: -0.9s;
}
.react5 {
animation-delay: -0.8s;
}
@keyframes waveLoading {
0% {
transform: scaleY(0.4);
}
20% {
transform: scaleY(1);
}
80% {
transform: scaleY(0.4);
}
100% {
transform: scaleY(0.4);
}
}
</style>
</html>