Reputation: 923
I am learning bootstrap 4 and I saw progress bar.So, i though to make it more iteractive using javascript. But,my progress bar is not working.I want to move the progress bar upto 100%,like we see normal progress bar moving from 0% to 100%. I tried :
var count=0;
var x=document.querySelector('.progress-bar');
window.onload=callMe();
;
function callMe(){
x.style.width=null;
setInterval(function(){
increaseBar();
},1000);
}
function increaseBar(){
count=count+10;
console.log(count);
console.dir(x);
x.style.width=count;
x.innerHTML=count;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Progress Bar With Label</h2>
<div class="progress">
<div class="progress-bar" style="width:0%">0%</div>
</div>
</div>
</body>
<script>
</script>
</html>
i see that x.style.width=count; is not working and i also tried using x.setAttribute("style,"width"),but it also didnot worked.
Upvotes: 1
Views: 1156
Reputation: 17570
You should use percentage for with and clear interwal when reach 100 as percentage
var count=0;var myVar;
var x=document.querySelector('.progress-bar');
window.onload=callMe();
;
function callMe(){
x.style.width=null;
myVar=setInterval(function(){
increaseBar();
},1000);
}
function increaseBar(){
if(count==100){clearInterval(myVar);return}
count=count+10;
x.style.width=count+"%";
x.innerHTML=count+"%";
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Progress Bar With Label</h2>
<div class="progress">
<div class="progress-bar" style="width:0%">0%</div>
</div>
</div>
</body>
<script>
</script>
</html>
Upvotes: 2