圓環百分比進度

           

.circle-progress-con{position: relative;margin: 0 auto;text-align: center;line-height: calc(196px - 16px);background-color: rgba(0, 0, 0, 0.6);border: 16px solid rgba(112, 112, 112, 0.5);color: #fff;font-size: 30px;}
.circle, .percent{position: absolute;}
.circle-progress-con, .circle, .percent{width: 196px;height: 196px;border-radius: 50%;box-sizing: border-box;}
.circle{top: -16px;left: -16px;clip:rect(0, 196px, 196px, calc(196px / 2));}
.clip-auto{clip: auto;/*clip:rect(auto, auto, auto, auto);*/}
.percent{border: 16px solid #ffdd00;}
.left{transition:transform ease;clip: rect(0, calc(196px / 2), 196px, 0);}
.right{clip:rect(0, 196px, 196px, calc(196px / 2));}
.wth0{width:0;}
<div class="circle-progress-con">
  <div class="circle">
    <div class="percent left"></div>
    <div class="percent right wth0"></div>
  </div>
  <div class="num"><span>0</span>%</div>
</div> 
var percent=0;
var loading=setInterval(function(){
  if(percent>100){
    percent=0;
    $('.circle').removeClass('clip-auto');
    $('.right').addClass('wth0');
  }else if(percent>50){
    $('.circle').addClass('clip-auto');
    $('.right').removeClass('wth0');
  }
  $('.left').css({'transform': 'rotate('+ (18/5)*percent +'deg)', '-webkit-transform': 'rotate('+ (18/5)*percent +'deg)', '-moz-transform': 'rotate('+ (18/5)*percent +'deg)'});
  $('.num>span').text(percent);
  percent++;
},200);

主要應用css的clip屬性:

剪裁形狀:高度是從top到bottom,寬度是從left到right

原理:用border畫圓環,利用剪裁剪出左右兩個半圓環,默認右半圓環width給0即不顯示,左半圓環顯示,之後放在一個大的圓形容器中,該容器利用剪裁默認只留右半部分來裝右半圓環,這樣默認左右半圓環都不會顯示出來,當百分比進度<=50的時候,漸漸向大容器右半部分旋轉左半圓環,這樣進度會從0%-50%(50%時與右半圓環重疊);當百分比進度>50的時候大容器不需要剪裁即露出整個圓形,右半圓環顯示出來(去掉width=0恢復寬度)繼續旋轉左半圓,即51%-100%(100%即左右圓環拼接成完整的圓環)。

 

css圓環實現方法:

1.border或box-shadow(box-shadow: [nset] 0 0 0 16px rgba(0, 0, 0, 0.5))

2.兩個圓嵌套或用before/after

 

參考:https://blog.csdn.net/angeljsl/article/details/51208960

其他方法:https://blog.csdn.net/xuyunfei_2012/article/details/53672876

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章