我正在研究一个模型,该模型将根据国家和百分比显示人口遗传人口构成。我已经对这些数据进行了计算。我需要一个能够解释这些数据并在页面上显示的解决方案。我试图达到的解决方案将执行以下操作:
根据从数据库中提取的数据创建一个Country数组,然后从现有颜色数组中指定颜色,color1 = country1 ...等等。将颜色分配给将颜色插入具有动态百分比的动态渐变(与国家/地区相关联的百分比)的国家/地区。
我已经探索了几种方法,没有可靠的解决方案。我的颜色数组包括10种颜色,所以我尝试了较少的路线,但是如何将颜色的分配传达给JS中的国家? (注意:我对我的代码做了一些更新!)
@yellow: #feea00;
@green1: #85c441;
@green2: #15ae4b;
.gradient (@color; @color2) {
background: -moz-linear-gradient(top, @color 0%, @color2 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,@color), color-stop(100%, @color2)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, @color 0%,@color2 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, @color 0%,@color2 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, @color 0%,@color2 100%); /* IE10+ */
background: linear-gradient(to bottom, @color 0%,@color2 100%); /* W3C */
-ms-filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='@color', endColorstr='@color2',GradientType=0 ); /* IE6-9 */
}
.cMatch img.green1 {
.gradient (@green1; (@green1 + #222));
}
.cMatch img.green2 {
.gradient (@green2; (@green2 + #222));
}
.cMatch img.blue1 {
.gradient (@blue1; (@blue1 + #222));
}
等等
我还想过在js中做一个动态样式,但我不确定我是否正确地处理了我需要的所有事情....我真的很喜欢基于jquery的解决方案并使用上面的代码少了,但我还处于试验阶段!以下是我的动态js尝试。
var CountryColor = ['feea00', '85c441', '15ae4b', '0078ff', '36c4bf', '392a7d', 'a02896', 'c52127', 'f09c20', 'e75725'];
$(document).ready(function () {
$('.Jewish').style.background = "webkit-gradient(linear, left top, left bottom, color-stop(0%," + CountryColor + "), color-stop(10%,#ffffff), color-stop(11%," + CountryColor + "), color-stop(22%,#ffffff), color-stop(23%," + CountryColor3 + "), color-stop(34%,#ffffff), color-stop(35%," + CountryColor4 + "), color-stop(45%,#ffffff), color-stop(46%," + CountryColor5 + "), color-stop(55%,#ffffff), color-stop(56%," + CountryColor6 + "), color-stop(66%,#ffffff), color-stop(67%," + CountryColor7 + "), color-stop(76%,#ffffff), color-stop(77%," + CountryColor8 + "), color-stop(83%,#ffffff), color-stop(84%," + CountryColor9 + "), color-stop(91%,#ffffff), color-stop(92%," + CountryColor10 + "), color-stop(100%,#ffffff));
});
渐变将具有1种颜色到10种颜色,具体取决于各国的细分百分比。
任何人都有最佳路线的建议将不胜感激。我的整个网站都是在asp.net和C#中完成的,所以如果有人对这条路线有建议,那也可以。谢谢!
答案 0 :(得分:1)
你与JS的例子非常接近 - 有一些问题:
您没有使用正确的数组语法来获取单个颜色。数组是0索引的,最容易访问,如ArrayName [i](所以,CountryColor [0]到CountryColor [9]
jQuery集合使用.css()方法http://api.jquery.com/css/来设置背景(不像你那样'风格',这是一个直接的JS事物。)
你应该避免在jQuery中使用裸类选择器,因为它们非常慢且效率低,要么使用ID(如果每个渐变是唯一的),要么使用标签名称限定选择器,或者首先找到按ID分类,然后在里面查找你想要的类。你可以做到这两点 - 例如:$('#yourContainer div.Jewish')
。
这个例子应该只是工作:
var CountryColor = ['#feea00', '#85c441', '#15ae4b', '#0078ff', '#36c4bf', '#392a7d', '#a02896', '#c52127', '#f09c20', '#e75725'];
$(document).ready(function () {
$('.Jewish').style.background = "webkit-gradient(linear, left top, left bottom, color-stop(0%," + CountryColor[0] + "), color-stop(10%,#ffffff), color-stop(11%," + CountryColor[1] + "), color-stop(22%,#ffffff), color-stop(23%," + CountryColor[2] + "), color-stop(34%,#ffffff), color-stop(35%," + CountryColor[3] + "), color-stop(45%,#ffffff), color-stop(46%," + CountryColor[4] + "), color-stop(55%,#ffffff), color-stop(56%," + CountryColor[5] + "), color-stop(66%,#ffffff), color-stop(67%," + CountryColor[6] + "), color-stop(76%,#ffffff), color-stop(77%," + CountryColor[7] + "), color-stop(83%,#ffffff), color-stop(84%," + CountryColor[8] + "), color-stop(91%,#ffffff), color-stop(92%," + CountryColor[9] + "), color-stop(100%,#ffffff));
});
如果使用JS对象存储颜色,可能会更容易阅读代码:
CountryColors = {
Afghanistan: '#00FFCC',
Albania: '#00FFCC',
Algeria: '#00FFCC',
American Samoa: '#00FFCC',
Andorra: '#00FFCC',
Angola: '#00FFCC',
Anguilla: '#00FFCC',
Antigua and Barbuda: '#00FFCC',
Argentina: '#00FFCC',
Armenia: '#00FFCC',
Aruba: '#00FFCC'
}
然后你可以在JS中按名称引用它们:
CountryColors.Afghanistan
最后,打包所有这些的一个好方法是创建一个简单的jQuery插件,它可以为你处理所有这些,并创建所有特定于浏览器的样式,插入白色的间隔符等。你可以这样称呼它:
$('div#Bob').countryColorFill({Albania: 15%, Angola: 45%, Aruba: 40%});
这个的实现相当简单,但可能有点偏离这个答案。