5

So I'm trying to animate multiple progress bars on one page, here is the HTML for my progress bars:

<div class="col-md-5">
  <p>HTML & CSS</p>
  <div class="progress progress-striped active">
    <div class="progress-bar progress-bar-danger" style="width: 50%;"></div>
  </div>
  <p>C#</p>
  <div class="progress progress-striped active">
    <div class="progress-bar progress-bar-danger" style="width: 20%;"></div>
  </div>
  <p>WordPress</p>
  <div class="progress progress-striped active">
    <div class="progress-bar progress-bar-danger" style="width: 30%;"></div>
  </div>
  <p>Photoshop</p>
  <div class="progress progress-striped active">
    <div class="progress-bar progress-bar-danger" style="width: 20%;"></div>
  </div>
</div>

I am also using this code in my CSS Styling

.progress.active .progress-bar {
   -webkit-transition: none !important;
   transition: none !important;
 }

And this is my JS function:

$(".progress-bar").animate({width: "10%"}, 2500);

Using this, all 4 bars are animated, but I'd like to animate each bar with a different value.

2 Answers 2

4

You can target the individual bars using .eq() which takes an index as a parameter to specify which bar you want.

$(".progress-bar").eq(0); // First bar
$(".progress-bar").eq(1); // Second bar

From here, just call the animate method on the selected element. Storing the initial $(".progress-bar") call is also considered good practice.

var $bars = $(".progress-bar");
$bars.eq(0).animate({width: "10%"}, 2500);
$bars.eq(1).animate({width: "5%"}, 2500);
Sign up to request clarification or add additional context in comments.

Comments

-2

this way you can also do it.

$( document ).ready(function() {
    $(".progress-bar").each(function (index ) {
        if(index >=0 && index<=1)
        {
            $(this).animate({width: "5%"}, 2500);
        }
    })
});    

http://jsfiddle.net/tridip/80cww0qr/

we can also increase progress bar randomly whose value will be between 1 to 100

$(this).animate({width: Math.floor((Math.random() * 100) + 1) + "%"}, 2500);

http://jsfiddle.net/tridip/80cww0qr/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.