简体   繁体   English

Bootstrap 3-基于值的自动背景色

[英]Bootstrap 3 - Automatic background color based on value

I'm looking for a way to change the background color of multiple columns based on a range of numeric values. 我正在寻找一种基于一定范围的数值来更改多列的背景色的方法。 I know of the Colorjizz PHP library but not sure if it's what I need for my purpose. 我知道Colorjizz PHP库,但是不确定是否正是我要的目的。 I need the columns in the code below to change their background color to the respective shade of green/red/orange based on a range of numeric values. 我需要以下代码中的列,以根据一定范围的数值将其背景色更改为相应的绿色/红色/橙色阴影。 For example from 1-100 or 0.1 to 10.0. 例如1-100或0.1到10.0。

 .green-bg { background-color: green!important; color: white; } .red-bg { background-color: red!important; color: white; } .orange-bg { background-color: orange!important; color: white; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="row"> <div class="col-md-6"> <div class="row"> <div class="col-md-12"> <div class="well green-bg">1 </div> </div> <div class="col-md-12"> <div class="well red-bg">3 </div> </div> </div> </div> <div class="col-md-6"> <div class="row"> <div class="col-md-12"> <div class="well orange-bg">2 </div> </div> <div class="col-md-12"> <div class="well green-bg">4 </div> </div> </div> </div> </div> </div> 

You could try grabbing the innerHTML of each div with the "well" class and then assuming that is a number between 0 and 10, set the opacity of the div to be that number divided by 10. So if the innerHTML is 2, then the opacity is set to 0.2. 您可以尝试使用“ well”类获取每个div的innerHTML,然后假定该数字介于0到10之间,将div的不透明度设置为该数字除以10。因此,如果innerHTML为2,则不透明度设置为0.2。

I've added some Javascript to your snippet to make this happen. 我在您的代码段中添加了一些Javascript以实现此目的。

You could change the math in this to make it 1-100 or something else. 您可以在其中更改数学以使其为1-100或其他。

 function wellColorShade() { var wells = document.getElementsByClassName("well"); for(var i = 0; i < wells.length; i++) { var well = wells[i]; well.style.opacity = (well.innerHTML / 10); } }; wellColorShade(); 
 .green-bg { background-color: green!important; color: white; } .red-bg { background-color: red!important; color: white; } .orange-bg { background-color: orange!important; color: white; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="row"> <div class="col-md-6"> <div class="row"> <div class="col-md-12"> <div class="well green-bg">1 </div> </div> <div class="col-md-12"> <div class="well red-bg">3 </div> </div> </div> </div> <div class="col-md-6"> <div class="row"> <div class="col-md-12"> <div class="well orange-bg">2 </div> </div> <div class="col-md-12"> <div class="well green-bg">4 </div> </div> </div> </div> </div> </div> 

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

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