简体   繁体   English

根据其宽度更改DIV背景颜色

[英]Change DIV Background Color, Depending on Its Width

I'm wanting to use jQuery to dynamically modify the background color of a <div> , based on its CSS property, width value. 我想基于它的CSS属性width值,使用jQuery动态修改<div>的背景色。

The usage is for some form of color-coded meter, which indicates how well (or poorly) a device performs in a specific category (there are 5), and then there is one 'overall' category, which produces the overall score, based on a little math (add all 5 together and divide the answer by 5). 该用法用于某种形式的彩色编码仪表,该仪表指示设备在特定类别(有5个)中的表现(或不佳),然后有一个“总体”类别,该类别会产生总得分,基于在一点数学上(将所有5加在一起,然后将答案除以5)。

I have tried two methods , one based on the little jQuery knowledge I have, and the other adapted from an answer on SO. 我尝试了两种方法 ,一种基于我所掌握的jQuery知识,另一种根据SO的答案改编而成。 Both are included in the JSFiddle I have provided, with the latter commented out. 两者都包含在我提供的JSFiddle中,而后者已被注释掉。

Here are the colors and the ranges of the widths for each: 以下是每种颜色的颜色和宽度范围:

  • 0-24% = red - #a41818 0-24%=红色-#a41818
  • 25-49% = orange - #87581c 25-49%=橙色-#87581c
  • 50-74% = yellow - #997815 50-74%=黄色-#997815
  • 75-90% = yellowgreen - #7ba01c 75-90%=黄绿色-#7ba01c
  • 91-100% = green - #3a8d24 91-100%=绿色-#3a8d24

Thanks! 谢谢!

I'd suggest: 我建议:

$('.rating-bar').css('background-color', function(){
    var percentage = parseInt($(this).data('size'), 10);
    if (percentage > 0 && percentage < 25){
        return '#a41818'
    }
    else if (percentage > 24 && percentage < 50) {
        return '#87581c';
    }
    else if (percentage > 49 && percentage < 75) {
        return '#997815';
    }
    else if (percentage > 74 && percentage < 90) {
        return '#7ba01c';
    }
    else if (percentage > 89 && percentage <= 100) {
        return '#3a8d24';
    }    
});

JS Fiddle demo . JS小提琴演示

There were a few semantic problems ( $(e) used instead of $(this) , ($(document).ready nested strangely), and the logic you've used requires the ratio of each bar's width to the parent bar, not the width itself. 存在一些语义问题(使用$(e)代替$(this)($(document).ready奇怪地嵌套),并且您使用的逻辑要求每个条的宽度与父条的宽度之比,而不是宽度本身。

Try this: http://jsfiddle.net/VFSUN/1/ 试试这个: http : //jsfiddle.net/VFSUN/1/

$(document).ready(function () {
    var bar = $('.rating-bar');
    bar.css("background-color", "#7ba01c");

    var parentWidth = parseInt($('.rating-bar-bg').css("width"));
    $('.rating-bar').each(function () {
        var e = $(this).css("width");
        e = parseInt(e);
        ratio = e / parentWidth * 100;
        if (ratio <= 24) {
            $(this).css("background-color", "#a41818");
        } else if (ratio >= 25 && e < 50) {
            $(this).css("background-color", "#87581c");
        } else if (ratio >= 50 && e < 75) {
            $(this).css("background-color", "#997815");
        } else if (e >= 75 && e < 91) {
            $(this).css("background-color", "#7ba01c");
        } else if (ratio >= 91) {
            $(this).css("background-color", "#3a8d24");
        }
    });
});

I suggest you are starting at wrong point by checking width, when in fact you need to be setting width based on the data-size attribute. 我建议您通过检查宽度来从错误的角度出发,而实际上您需要根据data-size属性设置宽度。 This size can then be used to set bckground color 然后可以使用此大小设置背景色

$(document).ready(function() {
  $('.rating-bar').each(function(){
        var $bar=$(this), size=$bar.data('size');
        $bar.width(size+'%').css("background-color", getBackground( size))
    });       
});    


 function getBackground( e){
    var  color= "#a41818";/* < 24*/
     if (e >= 25 && e < 50) {
             color= "#87581c";
        } else if (e >= 50 && e < 75) {
            color=  "#997815";
        } else if (e >= 75 && e < 91) {
            color=  "#7ba01c";
        } else if (e >= 91) {
            color=  "#3a8d24";
        }    
    return color

}

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM