Reputation: 211
i i'm trying to do an animated progress bar, and i have this code so far:
HTML
<div class="bar-container">
<div class="bar"><div class="bar-value"></div></div><button>Submit</button><img src="gif.gif" class="loader">
<br><br>
<span class="progress"></span>
</div>
JQUERY
$(document).ready(function(){
$('button').click(function(){
$('.loader').fadeIn(1200);
$('.bar-value').animate({width: '+=1%'},100);
$('.progress').text("1%");
$('.bar-value').animate({width: '+=1%'},100);
$('.progress').text("2%");
$('.bar-value').animate({width: '+=1%'},100);
$('.progress').text("3%");
});
});
The problem is that text() only change when all animations have finished so, you can't see (1%), (2%), (3%), etc, you just see (3%) when the three animations finish.
Thanks in advance
Upvotes: 0
Views: 75
Reputation: 97672
Actually it changes the text before the animation starts, if you want something to happen after an animation use the animation callback.
$(document).ready(function(){
$('button').click(function(){
$('.loader').fadeIn(1200);
$('.bar-value').animate({width: '+=1%'},100, function(){
$('.progress').text("1%");
});
$('.bar-value').animate({width: '+=1%'},100, function(){
$('.progress').text("2%");
});
$('.bar-value').animate({width: '+=1%'},100, function(){
$('.progress').text("3%");
});
});
});
Upvotes: 3