我正在构建一些html温度计,这些温度计是用jQuery动画显示的,以显示一个百分比(如您所看到的,我已经构建的Fiddle )。
我想要的,而且我不知道我做错了什么,就是让温度计的颜色根据它们所代表的水平改变。例如,在Fiddle的剧本中,80岁以下的应该是蓝色,80岁以上的应该是黄色,但它们都是蓝色的。
这是jQuery代码:
$('.lvl').each(function(){
var level = $(this).text();
$(this).animate( {
width: level+'%'
}, 1000);
var lvlwidth = $(this).width();
if (lvlwidth < 80) {
$(this).css('background-color', 'blue');
} else if (lvlwidth > 80) {
$(this).css('background-color', 'yellow');
}
});更新:正如我在下面的评论中所说的,我想要的是条形图在增长的时候动态地改变颜色,例如,当它小于40的时候是蓝色的,然后变成黄色,当它达到80的时候变成红色。
发布于 2014-11-28 22:45:08
更新:
http://jsfiddle.net/rpxr4bjj/3/
$('.lvl').each(function () {
var level = $(this).text();
$(this).animate({
width: level + '%',
}, {
progress: function () {
if (level < 80) {
$(this).css('background-color', 'blue');
} else if (level > 80) {
$(this).css('background-color', 'yellow');
}
}
}, 3000);
});https://stackoverflow.com/questions/27196557
复制相似问题