gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:41 25 4
gpt4 key购买 nike

我在一个招聘公告板上工作,招聘公告的数量总是在变化。但是,每个职位发布都应该有一种基色的分层阴影:#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com