2014-03-13 104 views
2

我正在工作板上工作发布数量不断变化。但是,每个工作职位应该有一个基本颜色的分层阴影:#219BBE。 这里是什么,我想实现一个草图:颜色阴影响应div的数量

Sketch of the shading concept

我需要的是一个JavaScript函数,它改变取决于job_box ES数量颜色的深浅。

到目前为止,我找到了一个用于生成#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_box ES我会使用量的jQuery:

var numBoxes = $('.job_box').length 

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

Fiddle

回答

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); 
} 

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

+0

太近了!只是想知道我怎样才能增加阴影差异... – Retador

+1

只需要用'var'来玩百分比。 – DonJuwe

+0

这个看起来很酷:http://jsfiddle.net/5KJD7/ - 虽然它应该是相反的... – Retador

3

看你的设计中,应该只有div的数量有限(比如4-8),所以我会亲自尽量保持简单和预先计算的背景颜色和实现它的8行CSS,而不是JavaScript的负载。

例如

DIV.job_box:nth-child(0) { background-color: #219BBE; } 
DIV.job_box:nth-child(1) { background-color: <nextcol>; } 
DIV.job_box:nth-child(2) { background-color: <nextcol>; } 
DIV.job_box:nth-child(3) { background-color: <nextcol>; } 
DIV.job_box:nth-child(4) { background-color: <nextcol>; } 
DIV.job_box:nth-child(5) { background-color: <nextcol>; } 
DIV.job_box:nth-child(6) { background-color: <nextcol>; } 

我知道这不是回答你的具体做法,但往往我们走这是要复杂得多,他们需要的路径。

+0

但是当4有更多的工作比1更多的时候你会怎么做?编辑:当作业总是排序,这肯定是更好的主意。 –

+0

非常好的主意 - 我高度专注于可加工性,但可能我们永远不会有999个工作机会。 :D – Retador

+0

即使是原版海报的作品也不可扩展,因为使用这样一个公式的颜色变化不会永远持续下去。无论是那个还是'我'循环将用完(例如,在每个代码示例10)。 –

1

尝试将作业计数追加到对象。

像这样:

<div class="job_box" data-count="22"></div> 
<div class="job_box" data-count="12"></div> 
<div class="job_box" data-count="5"></div> 
<div class="job_box" data-count="1000"></div> 

然后让所有框:

var numBoxes = $('.job_box'); 

环通过他们:

numBoxes.each(function(){ 
    var jobCount = $(this).data("count"); 
    $(this).css("background-color",'#'+calculateShades(jobCount)); 
}); 
+0

没错,这将是一个很好的数据统计应用。试图在小提琴中的代码,但它没有工作:http://jsfiddle.net/uAW2q/3/ – Retador

+0

你的JavaScript是无效的。 Javscript中没有'pad()'。但这不是问题的一部分。在这里你可以看到'calculateShades'工作不正确。 http://jsfiddle.net/uAW2q/4/ –