颜色阴影响应div的数量

Ret*_*dor 2 html javascript css colors

我正在一个工作委员会工作,其中有大量不断变化的职位发布.但是每个职位发布都应该有一种基色的分层色调:#219BBE.这是我想要实现的目标的草图:

阴影概念的剪影

我需要的是一个javascript函数,它根据job_boxes 的数量改变颜色的阴影.

到目前为止,我发现了一个用于生成阴影的javascript片段#219BBE.

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;
}
Run Code Online (Sandbox Code Playgroud)

我简化了这个问题的输出: 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>
Run Code Online (Sandbox Code Playgroud)

CSS

.job_box {
  width: 100%;
  height: 50px;

  /* The dynamic background-color */
  background-color: #219BBE;
}
Run Code Online (Sandbox Code Playgroud)

要计算job_boxes 的数量,我将使用jQuery:

var numBoxes = $('.job_box').length
Run Code Online (Sandbox Code Playgroud)

这就是我被卡住的地方.我知道我需要一个循环,但就是这样.你能帮助我朝正确的方向发展吗?

小提琴

Don*_*uwe 5

这是我的解决方案:DEMO

var n = 0;

$('.job_box').each(function() {
    n++;
    lighten($(this), n);
});

function lighten(that, n) {
    var lightenPercent = 15 / n;
    var rgb = that.css('background-color');
    rgb = rgb.replace('rgb(', '').replace(')', '').split(',');
    var red = $.trim(rgb[0]);
    var green = $.trim(rgb[1]);
    var blue = $.trim(rgb[2]);

    red = parseInt(red * (100 - lightenPercent) / 100);
    green = parseInt(green * (100 - lightenPercent) / 100);
    blue = parseInt(blue * (100 - lightenPercent) / 100);

    rgb = 'rgb(' + red + ', ' + green + ', ' + blue + ')';

    that.css('background-color', rgb);
}
Run Code Online (Sandbox Code Playgroud)

您可以在另一方面,通过改变变暗你的基本色/,以*设定的百分比时变种.