jQuery UI:如何更改ProgressBar的颜色?

时间:2009-09-25 10:33:31

标签: jquery jquery-ui progress-bar

我已经设置了一个简单的jQueryUI进度条:

<script type="text/javascript">
    $(function() {
        $("#progressbar").progressbar({
                value: 35
        });
    });
</script>

<div id="progressbar">  </div>

现在,我想根据它的值(例如&lt; 10 red,&lt; 50 yellow,&gt; 50 green)为条形颜色着色。我该怎么做?

注意:有similar questions,但答案不够明确,无法帮我完成任务。希望有人可以指出一种更简单的方法或提供更详细的说明。感谢。

5 个答案:

答案 0 :(得分:80)

我摆弄它,这就是我找到的。使用jQuery UI v1.8rc3,我可以覆盖进度条的主题颜色。

alt text http://i42.tinypic.com/mt5v6p.jpg

以下是: 将进度条小部件添加到div时,类似于:

$("#mydiv").progressbar({value:0});

... jQuery UI进度条在你的div中创建一个div;这个内部div表示值栏。要设置条形图的颜色,请设置背景 孩子(内心)div。

您还可以在进度条中设置空白区域的颜色,即值栏右侧的空白区域。通过设置外部div的背景来完成此操作。

对于其中任何一种,您可以使用平面颜色或图像。如果您使用图像,请务必设置repeat-x。执行此操作的代码如下所示:

HTML:

<div id='mainObj' class="inputDiv">
  <div id='pbar0' style="height: 20px;"></div>
  <div id='pbar1' style="height: 20px;"></div>
  <div id='pbar2' style="height: 20px;"></div>
  <div id='pbar3' style="height: 20px;"></div>
</div>

JS:

function init(){
    // four progress bars
    $("#pbar0").progressbar({ "value": 63 });
    $("#pbar1").progressbar({ "value": 47 });
    $("#pbar2").progressbar({ "value": 33 });
    $("#pbar3").progressbar({ "value": 21 });

    // the zero'th progressbar gets the default theme

    // set colors for progressbar #1
    $("#pbar1").css({ 'background': 'url(images/white-40x100.png) #ffffff repeat-x 50% 50%;' });
    $("#pbar1 > div").css({ 'background': 'url(images/lime-1x100.png) #cccccc repeat-x 50% 50%;' });

    // set colors for progressbar #2
    $("#pbar2").css({ 'background': 'url(images/lt-blue-40x100.png) #ffffff repeat-x 50% 50%' });
    $("#pbar2 > div").css({ 'background': 'url(images/dustyblue-1x100.png) #cccccc repeat-x 50% 50%' });

    // set colors for progressbar #3
    $("#pbar3").css({ 'background': 'LightYellow' });
    $("#pbar3 > div").css({ 'background': 'Orange' });
}

好的,它负责设置颜色。现在, 如果要在值更改时动态设置条形图的颜色,请挂钩progressbarchange事件,如下所示:

    $("#pbar0").bind('progressbarchange', function(event, ui) {
        var selector = "#" + this.id + " > div";
        var value = this.getAttribute( "aria-valuenow" );
        if (value < 10){
            $(selector).css({ 'background': 'Red' });
        } else if (value < 30){
            $(selector).css({ 'background': 'Orange' });
        } else if (value < 50){
            $(selector).css({ 'background': 'Yellow' });
        } else{
            $(selector).css({ 'background': 'LightGreen' });
        }
    });

工作示范:http://jsbin.com/atiwe3/3


注意:

如果要覆盖所有进度条的颜色,要使用的css类为ui-widget-content,“background”或外部div,ui-widget-header为实际bar(对应于内部div)。像这样:

  .ui-progressbar.ui-widget-content {
     background: url(images/white-40x100.png) #ffffff repeat-x 50% 50%;
  }

  .ui-progressbar.ui-widget-header {
     color: Blue;
     background: url(images/lime-1x100.png) #cccccc repeat-x 50% 50%;
  }

如果消除.ui-progressbar前缀,它将覆盖所有UI小部件的颜色,包括progressbars。

答案 1 :(得分:4)

使用以下代码:

$( "#nahilaga" ).progressbar({
     value: 20,
     create: function(event, ui) {$(this).find('.ui-widget-header').css({'background-color':'red'})}
   });

答案 2 :(得分:2)

jQuery Progressbar使用CSS和图像。

您的Stackoverflow答案说的相同:

  

有一个名为.ui-widget-overlay的css条目引用了图像ui-bg_diagonals-thick_20_666666_40x40.png,我认为这是实际驱动进度条的图像。您将不得不破解css,以便您可以在其他进度条中添加引用新图像的新类;我还没想出怎么做。

要更改颜色,您必须修改png图像。

或者如上所述,您可以复制图像,添加第二个类并使用jquery添加它们:

$(progressBar).addClass('secondImage');

答案 3 :(得分:0)

当您使用js中的值初始化进度条时,一件简单的事情就是:

$(progressBarId).children().css('backgroud',) ;

由于您需要为不同的进度条使用不同的颜色,您可以执行以下操作:

if($(progressBarId).value() <10 )
//set a color
if (..)
//set another color

我希望这能回答你的问题。我试着在第一个答案中做了那个人说的话,但是无法让它工作,所以尝试了这个并开始工作。

答案 4 :(得分:0)

因为关于已接受答案的工作示例似乎没有起作用,所以我会根据他的答案留下这个答案,以防有人发现它有用。

https://jsfiddle.net/benjamintorr/a1h9dtkf/

$(function() {
  $( ".progressbar" ).each(function(i, obj) {
    $( this ).progressbar({
     value: false
    });
    $( this ).bind('progressbarchange', function(event, ui) {
     updateColors( this );
    });
   });
  $( "button" ).on("click", function(event) {
    $( ".progressbar" ).each(function(i, obj) {
      $( this ).progressbar("option", {
        value: Math.floor(Math.random() * 100)
      });
    });
  });
});

function updateColors( progressBar ) {
  var value = $( progressBar ).progressbar("value");
  if ( value > 50 ) {
    progressColor = "green";
  } else if (value > 10) {
    progressColor = "#FF9900";
  } else {
    progressColor = "red";
  }
  $( progressBar ).find(".ui-progressbar-value").css("background", progressColor);
}
相关问题