【发布时间】:2014-03-13 10:12:09
【问题描述】:
我正在一个职位公告板上工作,职位发布的数量一直在变化。但是,每个职位发布都应具有一种基色的分层阴影:#219BBE。
这是我要实现的目标的草图:
我需要的是一个 javascript 函数,它可以根据 job_boxes 的数量改变颜色的深浅。
到目前为止,我找到了一个用于生成 #219BBE 阴影的 javascript sn-p。
function calculateShades(colorValue) {
"#219BBE" = colorValue;
// break the hexadecimal color value into R, G, B one-byte components
// and parse into decimal values.
// calculate a decrement value for R, G, and B based on 10% of their
// original values.
var red = parseInt(colorValue.substr(0, 2), 16);
var redDecrement = Math.round(red*0.1);
var green = parseInt(colorValue.substr(2, 2), 16);
var greenDecrement = Math.round(green*0.1);
var blue = parseInt(colorValue.substr(4, 2), 16);
var blueDecrement = Math.round(blue*0.1);
var shadeValues = [];
var redString = null;
var greenString = null;
var blueString = null;
for (var i = 0; i < 10; i++) {
redString = red.toString(16); // convert red to hexadecimal string
redString = pad(redString, 2); // pad the string if needed
greenString = green.toString(16); // convert green to hexadecimal string
greenString = pad(greenString, 2); // pad the string if needed
blueString = blue.toString(16); // convert blue to hexadecimal string
blueString = pad(blueString, 2); // pad the string if needed
shadeValues[i] = redString + greenString + blueString;
// reduce the shade towards black
red = red - redDecrement;
if (red <= 0) {
red = 0;
}
green = green - greenDecrement;
if (green <= 0) {
green = 0;
}
blue = blue - blueDecrement;
if (blue <= 0) {
blue = 0;
}
}
shadeValues[10] = "000000";
return shadeValues;
}
我简化了这个问题的输出: HTML
<!-- Instead of 4 boxes we could also have n boxes -->
<div class="job_box"></div>
<div class="job_box"></div>
<div class="job_box"></div>
<div class="job_box"></div>
CSS
.job_box {
width: 100%;
height: 50px;
/* The dynamic background-color */
background-color: #219BBE;
}
要计算job_boxes 的数量,我会使用 jQuery:
var numBoxes = $('.job_box').length
这就是我卡住的地方。我知道我需要一个循环,但就是这样。你能把我推向正确的方向吗?
【问题讨论】:
标签: javascript css html colors