2016-08-20 61 views
4

我想要一個半圈,如my fiddle所示。進度條需要有綠色。如何使用jQuery創建半圓進度條

我是Jquery的新手。

HTML:

<div> 
    <p>100%</p> 
</div> 

CSS:

div { 
    height: 45px; 
    width: 90px; 
    border-radius: 90px 90px 0 0; 
    -moz-border-radius: 90px 90px 0 0; 
    -webkit-border-radius: 90px 90px 0 0; 
    border: 5px solid red; 
    border-bottom: none; 
} 

p { 
    text-align: center; 
    padding: 20px 0; 
} 

Fiddle

+0

你需要使用一些JS插件做那。檢查此:https://github.com/kottenator/jquery-circle-progress – fernando

+0

只是試圖旋鈕jquery庫我跳它用完全你。 http://anthonyterrien.com/demo/knob/ –

+0

@Dhaval Thankyou!谷歌搜索!但沒有運氣,這就是爲什麼我在這裏問 – parish

回答

10

使用jQuery對任意值.animate()step控制.bar CSS3 transform: rotate

$(".progress").each(function(){ 
 
    
 
    var $bar = $(this).find(".bar"); 
 
    var $val = $(this).find("span"); 
 
    var perc = parseInt($val.text(), 10); 
 

 
    $({p:0}).animate({p:perc}, { 
 
    duration: 3000, 
 
    easing: "swing", 
 
    step: function(p) { 
 
     $bar.css({ 
 
     transform: "rotate("+ (45+(p*1.8)) +"deg)", // 100%=180° so: ° = % * 1.8 
 
     // 45 is to add the needed rotation to have the green borders at the bottom 
 
     }); 
 
     $val.text(p|0); 
 
    } 
 
    }); 
 
});
.progress{ 
 
    position: relative; 
 
    margin: 4px; 
 
    float:left; 
 
    text-align: center; 
 
} 
 
.barOverflow{ /* Wraps the rotating .bar */ 
 
    position: relative; 
 
    overflow: hidden; /* Comment this line to understand the trick */ 
 
    width: 90px; height: 45px; /* Half circle (overflow) */ 
 
    margin-bottom: -14px; /* bring the numbers up */ 
 
} 
 
.bar{ 
 
    position: absolute; 
 
    top: 0; left: 0; 
 
    width: 90px; height: 90px; /* full circle! */ 
 
    border-radius: 50%; 
 
    box-sizing: border-box; 
 
    border: 5px solid #eee;  /* half gray, */ 
 
    border-bottom-color: #0bf; /* half azure */ 
 
    border-right-color: #0bf; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="progress"> 
 
    <div class="barOverflow"> 
 
    <div class="bar"></div> 
 
    </div> 
 
    <span>10</span>% 
 
</div> 
 

 
<div class="progress"> 
 
    <div class="barOverflow"> 
 
    <div class="bar"></div> 
 
    </div> 
 
    <span>100</span>% 
 
</div> 
 

 
<div class="progress"> 
 
    <div class="barOverflow"> 
 
    <div class="bar"></div> 
 
    </div> 
 
    <span>34</span>% 
 
</div> 
 

 
<div class="progress"> 
 
    <div class="barOverflow"> 
 
    <div class="bar"></div> 
 
    </div> 
 
    <span>67</span>% 
 
</div>

PS:你需要JS(它在純CSS3可行的......除非你需要精確控制進步的階梯)

+2

不錯的做法:) – akinuri

+1

謝謝,這將工作!但是,是否有可能以紅色顯示全部邊界,並且只能以綠色顯示完成的過程? – parish

+0

[小提琴](https://jsfiddle.net/jf5jaohq/1/)檢查一下。我需要那條紅線上的'bar' div ..那條紅線永遠不會移動它保持它的狀態 – parish