I have a page with several bootstrap progress bars. Setting their values initially works fine. Though I would like the progress bars to animate/transition to their specific states when a user opens the page.
This JS works fine, when you click on one of the bars. I would need something like that on an "onload" event of the bar. But the "onload" event is not available for s
//animate progress bars
$('.progress .bar').on("click", function(event) {
var me = $(this);
perc = me.attr("data-percentage");
me.css('width', perc+'%');
});
How can I achieve this behavior on page load for all progress bars on a page?
While Tats_innit's answer has a nice touch to it, I had to do it a bit differently since I have more than one progress bar on the page.
here's my solution:
JSfiddle: http://jsfiddle.net/vacNJ/
HTML (example):
<div class="progress progress-success">
<div class="bar" style="float: left; width: 0%; " data-percentage="60"></div>
</div>
<div class="progress progress-success">
<div class="bar" style="float: left; width: 0%; " data-percentage="50"></div>
</div>
<div class="progress progress-success">
<div class="bar" style="float: left; width: 0%; " data-percentage="40"></div>
</div>
JavaScript:
setTimeout(function(){
$('.progress .bar').each(function() {
var me = $(this);
var perc = me.attr("data-percentage");
var current_perc = 0;
var progress = setInterval(function() {
if (current_perc>=perc) {
clearInterval(progress);
} else {
current_perc +=1;
me.css('width', (current_perc)+'%');
}
me.text((current_perc)+'%');
}, 50);
});
},300);
@Tats_innit: Using setInterval() to dynamically recalc the progress is a nice solution, thx mate! ;)
EDIT:
A friend of mine wrote a nice jquery plugin for custom twitter bootstrap progress bars. Here's a demo: http://minddust.github.com/bootstrap-progressbar/
Here's the Github repo: https://github.com/minddust/bootstrap-progressbar
EDIT
bar
to progress-bar
in v3.1.1HTML
<div class="container">
<div class="progress progress-striped active">
<div class="bar" style="width: 0%;"></div>
</div>
</div>
CSS
@import url('http://twitter.github.com/bootstrap/assets/css/bootstrap.css');
.container {
margin-top: 30px;
width: 400px;
}
jQuery used in the fiddle below and on the document.ready
$(document).ready(function(){
var progress = setInterval(function() {
var $bar = $('.bar');
if ($bar.width()>=400) {
clearInterval(progress);
$('.progress').removeClass('active');
} else {
$bar.width($bar.width()+40);
}
$bar.text($bar.width()/4 + "%");
}, 800);
});
Demo
:)
Tats_innit 2014-12-02 22:28
Bootstrap uses CSS3 transitions so progress bars are automatically animated when you set the width of .bar trough javascript / jQuery.
http://jsfiddle.net/3j5Je/ ..see?
In contribution to ellabeauty's answer. you can also use this dynamic percentage values
$('.bar').css('width', function(){ return ($(this).attr('data-percentage')+'%')});
And probably add custom easing to your css
.bar {
-webkit-transition: width 2.50s ease !important;
-moz-transition: width 2.50s ease !important;
-o-transition: width 2.50s ease !important;
transition: width 2.50s ease !important;
}
Here's a cross-browser CSS-only solution. Hope it helps!
.progress .progress-bar {
-moz-animation-name: animateBar;
-moz-animation-iteration-count: 1;
-moz-animation-timing-function: ease-in;
-moz-animation-duration: .4s;
-webkit-animation-name: animateBar;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: ease-in;
-webkit-animation-duration: .4s;
animation-name: animateBar;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: .4s;
}
@-moz-keyframes animateBar {
0% {-moz-transform: translateX(-100%);}
100% {-moz-transform: translateX(0);}
}
@-webkit-keyframes animateBar {
0% {-webkit-transform: translateX(-100%);}
100% {-webkit-transform: translateX(0);}
}
@keyframes animateBar {
0% {transform: translateX(-100%);}
100% {transform: translateX(0);}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<h3>Progress bar animation on load</h3>
<div class="progress">
<div class="progress-bar progress-bar-success" style="width: 75%;"></div>
</div>
</div>