Learn how to create a progress bar using JavaScript.
<div id="myProgress">
<div id="myBar"></div>
</div>
#myProgress {
width: 100%;
background-color: grey;
}
#myBar {
width: 1%;
height: 30px;
background-color: green;
}
Create a Dynamic Progress Bar (animated) Using JavaScript:
var i = 0;
function move() {
if (i == 0) {
i = 1;
var elem = document.getElementById("myBar");
var width = 1;
var id = setInterval(frame, 10);
function frame() {
if (width >= 100) {
clearInterval(id);
i = 0;
} else {
width++;
elem.style.width = width + "%";
}
}
}
}
Try it Yourself »
If you want to add labels to indicate how far the user is in the process, add a new element inside (or outside) the progress bar:
<div id="myProgress">
<div id="myBar">10%</div>
</div>
#myBar {
width: 10%;
height: 30px;
background-color: #04AA6D;
text-align: center; /* To center it horizontally (if you want) */
line-height: 30px; /* To center it vertically */
color: white;
}
Try it Yourself »
If you want to dynamically update the text inside the label to the same value of the width of the progress bar, add the following:
var i = 0;
function move() {
if (i == 0) {
i = 1;
var elem = document.getElementById("myBar");
var width = 10;
var id = setInterval(frame, 10);
function frame() {
if (width >= 100) {
clearInterval(id);
i = 0;
} else {
width++;
elem.style.width = width + "%";
elem.innerHTML = width + "%";
}
}
}
}
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!