代码之家  ›  专栏  ›  技术社区  ›  Mr. Developer

基于宽度百分比,使用css或jquery在进度栏上显示多种颜色

  •  3
  • Mr. Developer  · 技术社区  · 8 年前

    我需要用CSS、JS或您建议的方式实现一个进度条。 我需要它是多色的。。。基于百分比。

    例如,如果我有一个50%的值,进度条应该是浅蓝色到30%,绿色在30%到50%之间。

    所以,我设定 style="width:xx%" 设置百分比。

    是否有创建多色进度条的方法?

    enter image description here

    我试过这个:

    .progressMulti {
        height: 32px;
        width: 100%;
        border: 1px solid #ddd;
        border-radius: 15px;
        text-align: center;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        color: #333;
        font-size: 20px;
        background: linear-gradient(to right, green 75%, blue 20%, red 5%);
    }
    

    但如果设置为宽度50%,例如,我可以看到所有颜色。。。相反,它应该只为浅蓝色和绿色,值为50%。

    2 回复  |  直到 8 年前
        1
  •  2
  •   mscdeveloper    8 年前

    function set_progress(_num){
    	$('#progress').empty();
    	var el_1_width=0;
    	var el_2_width=0;
    	var el_3_width=0;
    	var el_4_width=0;
    	if(_num>30){el_1_width=30;}else{el_1_width=_num;}
    	if(_num>60){el_2_width=30;}else{el_2_width=_num-el_1_width;}
    	if(_num>80){el_3_width=30;}else{el_3_width=_num-el_1_width-el_2_width;}
    	if(_num>90){el_4_width=_num-90;}
    	var new_font_clor='';
    	if(_num<55){new_font_clor='color:black';}
    	$('#progress').append('<div class="progress-text" style="'+new_font_clor+'">'+_num+' %</div>');
    	$('#progress').append('<div class="progress-el" style="background-color:blue; width:'+el_1_width+'%;">&nbsp;</div>');
    	$('#progress').append('<div class="progress-el" style="background-color:green; width:'+el_2_width+'%;">&nbsp;</div>');
    	$('#progress').append('<div class="progress-el" style="background-color:yellow; width:'+el_3_width+'%;">&nbsp;</div>');
    	$('#progress').append('<div class="progress-el" style="background-color:red; width:'+el_4_width+'%;">&nbsp;</div>');
    
    }
    
    set_progress(12);
       #progress{
         border-style:solid;
         border-color:black;
         //border-width:4px;
       }
       .progress-el{
       	 display:inline-block;
       	 height:40px;
       }
       .progress-text{
         position:fixed;
         color:white;
         font-weight:bold;
         width:100%;
         height:40px;
         line-height:40px;
         text-align:center;
         font-size:24pt;
    
       }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    
     <div id="progress"></div>
    
    
     <input type="button" value="set 17%" onclick="set_progress(17);">
     <input type="button" value="set 31%" onclick="set_progress(31);">
     <input type="button" value="set 45%" onclick="set_progress(45);">
     <input type="button" value="set 67%" onclick="set_progress(67);">
     <input type="button" value="set 82%" onclick="set_progress(82);">
     <input type="button" value="set 97%" onclick="set_progress(97);">
        2
  •  0
  •   Anurag Sinha    8 年前

    使用HTML画布如何?参考 https://jsfiddle.net/y8m8pk1m/ 例如。

    <!DOCTYPE html>
    <html>
    <body>
    
    <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.</canvas>
    
    <script>
    
    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    ctx.fillStyle = "blue";
    ctx.fillRect(20, 20, 70, 20);
    ctx.stroke();
    
    ctx.fillStyle = "red";
    ctx.fillRect(90, 20, 50, 20);
    ctx.stroke();
    
    ctx.fillStyle = "black";
    ctx.fillRect(140, 20, 50, 20);
    ctx.stroke();
    
    </script> 
    
    </body>
    </html>

    如果您正在寻找有关此参考的稍微复杂的示例, https://github.com/anurag-sinha/utils/tree/master/widgets/Gauges/analog 这是一个表示标量的模拟仪表(您可以下载并在Chrome中运行Analog.html)。你基本上只需要这个仪表上的彩色阈值条。模拟仪表有点复杂,因为它涉及一些计算旋转角度等的算法,但在您的情况下应该相当简单(使用fillRect)。您可以在类似的行上构建小部件。