简体   繁体   English

通过javascript为Twitter Bootstrap动态更改进度条的颜色

[英]Dynamically change the color of progress bar by javascript for Twitter Bootstrap

Bootstrap has many pre-defined styles for progress bar, but how to make them some dynamic changing feature. Bootstrap有许多用于进度条的预定义样式,但是如何使它们具有一些动态变化功能。 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. 由于我对javascript(codeacademy的第一阶段)知之甚少,无法通过宽度样式更改动态地更改它们,这就是问题所在。

<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". 需要通过javascript定义'value',如果“bar”样式宽度> 50%,当删除父类“bar-success”并添加类“bar-warning”时,样式宽度> 80%将其更改为“bar-danger” ”。

found smth like this with jquery http://jsfiddle.net/ZQrnC/ but think it would be better to have such a script for pure js to use it in bootstrap. 用jquery http://jsfiddle.net/ZQrnC/发现这样的smth,但是认为让纯js的这样一个脚本在bootstrap中使用它会更好。

in this question also reccomended to use addClass but this seems too difficult for me 这个问题中也建议使用addClass但这对我来说似乎太难了

here some bootstrap progress bar markup http://jsfiddle.net/pZ5mG/ 这里有一些bootstrap进度条标记http://jsfiddle.net/pZ5mG/

Thank you! 谢谢!

Here's a strictly vanilla.js solution: http://jsfiddle.net/pZ5mG/2/ 这是一个严格的vanilla.js解决方案: http//jsfiddle.net/pZ5mG/2/

HTML HTML

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

JS 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 accomplish? 这是关于你想要完成的事情吗? (Obviously you wouldn't be using an Interval to update the progress) (显然你不会使用Interval来更新进度)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM