javascript - 颜色阴影响应 div 的数量

标签 javascript css html colors

我在一个招聘公告板上工作,招聘公告的数量总是在变化。但是,每个职位发布都应该有一种基色的分层阴影:#219BBE。 这是我要实现的目标的草图:

Sketch of the shading concept

我需要的是一个 javascript 函数,它根据 job_boxes 的数量改变颜色的深浅。

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

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

这就是我卡住的地方。我知道我需要一个循环,但仅此而已。你能把我推向正确的方向吗?

Fiddle

最佳答案

这是我的解决方案: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);
}

另一方面,在设置百分比变量时,您可以通过将 / 更改为 * 来加深基色。

关于javascript - 颜色阴影响应 div 的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22375253/

相关文章:

Javascript:有什么比我现在使用的更好的将数组拼接在一起的方法?

文本填充 100% 高度的 css 图像

html - 在 DIV 容器内的类中维护图像的比率

html - 在 CSS/HTML 中创建固定/粘性 Logo

javascript - javascript/css 中的顺序动画

html - 如何设置 iOS6 文件输入控件的样式?

javascript - Firefox DOMContentLoaded 事件问题

javascript - 从对象内部的对象中删除键

javascript - js中同步管理缓存

当父级 <li> 的焦点丢失时,CSS 下拉列表消失