<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<style>
#myProgress {
width: 100%;
height: 30px;
position: relative;
background-color: #ddd;
}
#myBar {
background-color: #4CAF50;
width: 10px;
height: 30px;
position: absolute;
}
</style>
<body>
<h1>JavaScript 进度条</h1>
<div id="myProgress">
<div id="myBar"></div>
</div>
<br>
<button onclick="move()">点我</button>
<script>
var elem = document.getElementById("myBar");
var width = 0;
function move() {
var id = setInterval(frame, 10);
function frame() {
if (width == 100) {
clearInterval(id);
stic()
} else {
width++;
elem.style.width = width + '%';
console.log(width)
}
}
}
function stic() {
var id1 = setInterval(reduce, 10)
function reduce() {
if (width == 0) {
clearInterval(id1);
move()
} else {
width--;
elem.style.width = width + '%';
console.log(width)
}
}
}
</script>
</body>
</html>