プログレス バー効果は Web サイトで非常に一般的です。たとえば、Web サイトを初めて開くとプログレス バーが表示され、読み込みが完了すると Web ページのコンテンツが表示されます。
実装コードはここに直接添付されています
✨html部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
#progress {
border: 1px solid #000;
margin: 200px auto;
width: 502px;
height: 30px;
border-radius: 20px;
overflow: hidden;
box-shadow: 2px 3px 10px;
}
#son {
background: skyblue;
width: 0;
height: 30px;
text-align: center;
line-height: 30px;
}
</style>
</head>
<body>
<div id="progress">
<div id="son">0%</div>
</div>
<script src="index.js"></script>
</body>
</html>
✨JavaScript部分
let progress = document.getElementById('progress'),
son = document.getElementById('son'),
speed = 0;
let timer = setInterval(() => {
if (son.style.width == '100%') {
clearInterval(timer)
} else {
son.innerText = speed + '%'
son.style.width = speed + '%'
speed++
}
}, 30)