How to change the color of a bootstrap (twitter) progress bar at runtime

后端 未结 4 452
广开言路
广开言路 2020-12-09 18:06

I am using this progress bar :

http://twitter.github.com/bootstrap/components.html#progress

And would like to give it a custom color, known only at runtime (

相关标签:
4条回答
  • 2020-12-09 18:44

    There is now a bootstrap 3.2 progress bar designer tool.

    http://bootstrapdesigntools.com/tools/bootstrap-progress-bar-designer/

    0 讨论(0)
  • 2020-12-09 18:47

    You should change the container div class in order to change the color.

    Example using .progress-danger for red color:

    <div class="progress progress-danger">
      <div class="bar" style="width: 60%;"></div>
    </div>
    

    More colors (just substitute button for progress in the class name). http://twitter.github.com/bootstrap/base-css.html#buttons

    Update: In order to add the class name at runtime with javascript take a look at http://snipplr.com/view/2181/ or http://api.jquery.com/addClass/ if you are using jQuery.

    Hope it helps.

    0 讨论(0)
  • 2020-12-09 18:55

    Do you mean how to change the colours into one another at runtime?

    I can only imagine you do as you have not marked anyone as being correct. In the case you do mean this, then have a look at this very minimal jsFiddle

    HTML

    <div class="progress">
        <div class="bar bar-success" style="width: 0%; opacity: 1;"></div>
    </div>
    

    JS

    jQuery(document).ready( function(){
        window.percent = 0;
        window.progressInterval = window.setInterval( function(){
            if(window.percent < 100) {
                window.percent++;
                jQuery('.progress').addClass('progress-striped').addClass('active');
                jQuery('.progress .bar:first').removeClass().addClass('bar')
                .addClass ( (percent < 40) ? 'bar-danger' : ( (percent < 80) ? 'bar-warning' : 'bar-success' ) ) ;
                jQuery('.progress .bar:first').width(window.percent+'%');
                jQuery('.progress .bar:first').text(window.percent+'%');
            } else {
                window.clearInterval(window.progressInterval);
                jQuery('.progress').removeClass('progress-striped').removeClass('active');
                jQuery('.progress .bar:first').text('Done!');
            }
        }, 100 );
    });
    

    http://jsfiddle.net/LewisCowles1986/U9xw6/

    0 讨论(0)
  • 2020-12-09 18:57

    Your code is actually working, just that the progress bar is actually using a gradient as a color instead of a solid background-color property. In order to change the background color, set the background-image to none and your color will be picked up:

    $('#pb').css({
        'background-image': 'none',
        'background-color': 'red'
    });
    
    0 讨论(0)
提交回复
热议问题