CSS仅为表格行宽度的百分比设置背景颜色

时间:2012-08-18 18:35:51

标签: jquery css twitter-bootstrap html-table

我有一个包含两列的基本表:名称和值。我想根据值的大小为表中的每一行加上适当的宽度百分比(基本上创建一个横向直方图!)。我可以编写代码来计算要设置的适当百分比,但我无法弄清楚CSS是否能够适当地对每一行进行着色。似乎整行可以加阴影,但不是百分比。这是真的吗?

对于它的价值,我正在使用Twitter Bootstrap,如果需要我也可以使用jQuery。这只能在Chrome上运行,因此CSS3& Webkit只是没问题!这是HTML:

<table class="table">
  <tbody class="lead">              
    <tr> 
      <td>
        Joe
      </td>
      <td>
        10
      </td>
    </tr>              
    <tr> 
      <td>
        Jane
      </td>
      <td>
        20 
      </td>
    </tr>              
    <tr> 
      <td>
        Jim
      </td>
      <td>
        2
      </td>
    </tr>
  </tbody>
</table>

有关如何实现这一目标的任何提示?希望这个问题有道理。

2 个答案:

答案 0 :(得分:23)

您可以使用线性渐变。

如果百分比是40%:

table{
    background: -webkit-gradient(linear, left top, right top, color-stop(40%,#F00), color-stop(40%,#00F));
    background: -moz-linear-gradient(left center, #F00 40%, #00F 40%);
    background: -o-linear-gradient(left, #F00 40%, #00F 40%);
    background: linear-gradient(to right, #F00 40%, #00F 40%);
}

Demo

所以,在JavaScript中,

var percentage=40,
    col1="#F00",
    col2="#00F";
var t=document.getElementById('table');
t.style.background = "-webkit-gradient(linear, left top,right top, color-stop("+percentage+"%,"+col1+"), color-stop("+percentage+"%,"+col2+"))";
t.style.background = "-moz-linear-gradient(left center,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";
t.style.background = "-o-linear-gradient(left,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";
t.style.background = "linear-gradient(to right,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";

Demo


如果您想以不同方式将其应用于每一行:

var col1="#F00",
    col2="#00F";
var els=document.getElementById('table').getElementsByTagName('tr');
for (var i=0; i<els.length; i++) {
    var percentage = Number(els[i].getElementsByTagName('td')[1].firstChild.nodeValue);
    els[i].style.background = "-webkit-gradient(linear, left top,right top, color-stop("+percentage+"%,"+col1+"), color-stop("+percentage+"%,"+col2+"))";
    els[i].style.background = "-moz-linear-gradient(left center,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)" ;
    els[i].style.background = "-o-linear-gradient(left,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";
    els[i].style.background = "linear-gradient(to right,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)" ;
}

问题是将背景设置为tr在Firefox和Opera上运行良好,但在Chrome上,渐变应用于每个单元格。

添加此代码可以解决此问题(请参阅https://stackoverflow.com/a/10515894):

#table td {display: inline-block;}

Demo

答案 1 :(得分:11)

无需使用渐变色。首先,使用要为行着色的颜色创建一个简单的图像文件。就我而言,我创建了一个.png,它是全黑色的(下面示例中为black.png)。现在,只需使用background-imagebackground-size属性为行的相应部分着色。

示例,HTML:

<table cellspacing = "0px">
    <tr class = "row"><td>Hello Hello</td><td>Bye Bye</td></tr>
</table>

CSS:

.row {
    background-color: white; /*fallback color*/
    background-image: url(black.png);
    background-size: 75% 100%; /*your percentage is the first one (width), second one (100%) is for height*/
    background-repeat: no-repeat;
    color: rgb(0, 140, 200);
    font-weight: bold;
}

结果:

Result

以下是一个关于如何为您的示例自动执行此操作的简单代码:

var tbl = document.getElementsByClassName("table")[0];
var rws = tbl.rows;
for(var i = 0; i < rws.length; i ++) {
    var percentage = parseInt(rws[i].getElementsByTagName("td")[1].innerHTML, 10);
    rws[i].style.backgroundImage = "black.png";
    rws[i].style.backgroundSize = percentage + "%" + " 100%";
    rws[i].style.backgroundRepeat = "no-repeat";
    rws[i].style.color = "rgb(0, 140, 200)";
}

小演示:percentage width bg color (non-gradient based)

我希望有所帮助!