Dynamically Change Bar Percentage

Asked

Viewed 158 times

3

I have this bar percentage and I wanted to dynamically change the value of it only I’m not getting it.

inserir a descrição da imagem aqui

Bar code

<script>
$('.bar-percentage[data-percentage]').each(function () {
  var progress = $(this);
  var percentage = Math.ceil($(this).attr('data-percentage'));
  $({countNum: 0}).animate({countNum: percentage}, {
    duration: 2000,
    easing:'linear',
    step: function() {
      // What todo on every count
      var pct = Math.floor(this.countNum) + '%';
      progress.text(pct) && progress.siblings().children().css('width',pct);
    }
  });
});
</script>
<div id="bar-5" class="bar-main-container">
              <div class="bar-percentage" data-percentage="51"></div>
              <div class="bar-container">
                <div class="bar"></div>
            </div>
          </div>

Code I used to try to change Dynamically

<script>
$('.bar-percentage').attr('data-percentage','60')//irá receber um valor diferente depois de efectuar cálculos
</script>

1 answer

2


I reworked your function to give a load in progressBar, take a look:

I created the function changePercentage(), on it you inform the progress bar that you want the load. If you only tell progressBar, it will capture the new bar value by the attribute data-percentage, if you want a new value other than that of this attribute just put in the second argument of the function.

Example:

$(document).ready(function() {
  function changePercentage(progress) {
    var percentage = arguments.length > 1 ? arguments[1] : progress.attr('data-percentage')
    percentage = Math.floor(percentage);
    var bar = progress.siblings().children();
    bar.stop().animate({
      width: percentage * progress.width() / 100
    }, {
      duration: 2000,
      easing: 'linear',
      step: function() {
        var cdt = Math.round(100 * bar.width() / progress.width());
        progress.text(cdt + "%");
      }
    });
  }

  changePercentage($('.bar-percentage')); // Captura o novo valor do data-percentage

  $('button').click(function() {
    changePercentage($('.bar-percentage'), 10); // Captura o valor do segundo argumento (10)
  });
})
.bar-container {
  width: 100%;
  background: #c7c7c7;
  height: 30px;
}

.bar-container .bar {
  background: #333;
  height: 30px;
  width: 0
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="bar-5" class="bar-main-container">
  <div class="bar-percentage" data-percentage="51"></div>
  <div class="bar-container">
    <div class="bar"></div>
  </div>
</div>

<br>

<button>Mudar porcentagem para 10%</button>

  • Thank you very much!

  • You’re welcome! I’m glad I could help.

  • :) I tried to integrate this into my code and does not load the black bar ;(

  • @MANIAMAX, I THINK I KNOW THE PROBLEM, I WILL EDIT THE ANSWER WITH A POSSIBLE SOLUTION

  • ok :) appears as http://i1069.photobucket.com/albums/u479/thepeterpah/Captura%20de%20ecra%202015-12-28%20as%2015.17.41_zpspihayoh5.png

  • You created some way to get the bar to load with a onload, am o glued the code?

  • Add a $(document).ready(.., as in my reply issue.

  • even so the platform in question does not work is developed in extj and does not accept the ready Document

  • I have two questions: Does the button function work? And in the example of your code, which is in question, the first case with the $('.bar-percentage[data-percentage]').each( worked?

  • Yes with the $('.bar-percentage[data-percentage]').each( works with the button also does not work

  • Try $('.bar-percentage[data-percentage]').each( changePercentage($('.bar-percentage')))

  • does not work in the same but thanks for the help I will see another way and then put here the solution

  • Anyway, excuse me, I took into consideration only what was in the question. But, good luck

  • no need to apologize was a big help :)

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.