最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Dynamically change the color of progress bar by javascript for Twitter Bootstrap - Stack Overflow

programmeradmin1浏览0评论

Bootstrap has many pre-defined styles for progress bar, but how to make them some dynamic changing feature. With my poor knowledge of javascript (first stages on codeacademy), can't make them change dynamically by width style changing, and that's the question.

<div class="progress bar-success">
<div class="bar" style="width: 'value'%"></div>
</div>

need to define by javascript if 'value', if "bar" style width >50% when remove parent class "bar-success" and add class "bar-warning", when style width >80% change it to "bar-danger".

found smth like this with jquery / but think it would be better to have such a script for pure js to use it in bootstrap.

in this question also recended to use addClass but this seems too difficult for me

here some bootstrap progress bar markup /

Thank you!

Bootstrap has many pre-defined styles for progress bar, but how to make them some dynamic changing feature. With my poor knowledge of javascript (first stages on codeacademy), can't make them change dynamically by width style changing, and that's the question.

<div class="progress bar-success">
<div class="bar" style="width: 'value'%"></div>
</div>

need to define by javascript if 'value', if "bar" style width >50% when remove parent class "bar-success" and add class "bar-warning", when style width >80% change it to "bar-danger".

found smth like this with jquery http://jsfiddle/ZQrnC/ but think it would be better to have such a script for pure js to use it in bootstrap.

in this question also recended to use addClass but this seems too difficult for me

here some bootstrap progress bar markup http://jsfiddle/pZ5mG/

Thank you!

Share Improve this question edited May 23, 2017 at 12:30 CommunityBot 11 silver badge asked Aug 30, 2012 at 20:45 user1637019user1637019 131 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Here's a strictly vanilla.js solution: http://jsfiddle/pZ5mG/2/

HTML

<div class="progress">
    <div id="myProgress" class="bar bar-danger"></div>
</div>​

JS

var bar = document.getElementById("myProgress");
var progress = 0;


function setProgress(percent){
    bar.style.width = percent + "%";

    if (percent > 90)
        bar.className = "bar bar-success";
    else if (percent > 50)
        bar.className = "bar bar-warning";
}

var interval = setInterval(
    function(){
        setProgress(++progress);
        if (progress == 100) window.clearInterval(interval);
    }, 100);

Is this about what you are wanting to acplish? (Obviously you wouldn't be using an Interval to update the progress)

发布评论

评论列表(0)

  1. 暂无评论