我正在制作一个事件列表,其日期转换为事件发生的天数。我希望活动有不同的颜色,更多的是更红,更接近一个更绿。 days是整数值,我事先检查它是否为null或低于0然后它是0.由于某种原因,有时某些事件会跳过此函数。
这个getTimeAndColorForElement()是列表中每个元素的内部循环。
function getTimeAndColorForElement(time, element){
//time can be ISO 8601-format
var t = Date.parse(time) - Date.parse(new Date());
var minutes = Math.floor( (t/1000/60) % 60 );
var hours = Math.floor( (t/(1000*60*60)) % 24 );
var days = Math.floor( t/(1000*60*60*24) );
if(!days || days < 0){
days = 0;
}
var color = getColor(days);
$(element).css('color', color);
//... then sets days, hours mins and so on..
}
调用此函数来获取颜色..
function getColor(days) {
var red = 0;
var green = 250;
var blue = 0;
//set color more red the more days there is
for(i = 0; i < days; i++) {
green -= 15;
red += 15;
}
//keeping it in 0-255 color values.
if(green < 0) {
green = 0;
}
if(red < 0) {
red = 0;
}
if(blue < 0) {
blue = 0;
}
if(green > 250) {
green = 250;
}
if(red > 250) {
red = 250;
}
if(blue > 250) {
blue = 250;
}
//color in rgb
var color = "'rgb(" + red + "," + green + "," + blue + ")'";
return color;
}
答案 0 :(得分:3)
为什么你不这样做:
var colorStep = 15,
red = colorStep * days,
green = 250 - colorStep * days,
blue = 0;
...