码库记事本

码库记事本

使用CSS3实现环形进度条效果

小诸哥 0

这篇文章主要介绍了使用CSS3实现环形进度条效果,需要的朋友可以参考下

进度条效果如下:

这里写图片描述

整圆的效果处理会简单些,不完整环实现起来细节多点。下边是实现逻辑和过程。

进度条组成:

环形:两种颜色进度条,当余量少于50%时进度条颜色由绿色变为黄色。进度条结构由两个叠加环形组成;上叠加环形展示剩余量,深色;下叠加环形展示100%总量,浅色。

样式实现:

1:画一个方形,如图中阴影部分所示:

这里写图片描述 

2:方形中画两个等大均分方形的矩形,(注意每个矩形一定要设置:overflow:hidden)如图中阴影部分所示:

这里写图片描述这里写图片描述

3:进度条由两个叠加环形组成,所以第一步的方形中需要画四个等大的矩形用来展示不同部分的环形。

4:每个矩形中画一个和父级方形等大的方形,用来展示环形,左半矩形中的环形只设置上边框和左边框;右半矩形中的环形只设置上边框和右边框,如图中阴影所示:

这里写图片描述

5:实现进度条的动态百分比进度,使用css3的transform:rotate将上叠加环形根据实际百分比换算成实际的旋转角度来实现。

 当剩余量大于50%时左侧上叠加环形旋转角度不用变,只有计算右测上叠加环形旋转角度即可。

 当剩余量小于百分之50%时,左侧上叠加环形旋转时,就会将左半环形展示为完整的半环,此时就需要一个用来遮挡左侧超出进度范围环形部分的左侧环;如下图所示:

这里写图片描述

html代码如下:

  1. <div class="progress_wrap js_halfClassNameObj">
  2.      <div class="right under">
  3.          <div class="circleProgress rightcircle"></div>
  4.      </div>
  5.      <div class="left under">
  6.          <div class="circleProgress leftcircle"></div>
  7.      </div>
  8.      <div class="right up">
  9.          <div class="circleProgress rightcircle js_progressRight" style="-webkit-transform:rotate('+circleData.rightRotate.toString()+'deg)"></div>
  10.      </div>
  11.      <div class="left up">
  12.          <div class="circleProgress leftcircle js_progressLeft" style="-webkit-transform:rotate('+circleData.leftRotate.toString()+'deg)"></div>
  13.      </div>
  14.      //percent小于50时需要使用遮罩进行遮挡超出环形范围部分
  15.      <div class="left up_left_cover js_giftLeftCover" style="display:'+circleData.leftCircleDisplay+'">
  16.          <div class="circleProgress leftcircle color_border_t_l04"></div>
  17.      </div>
  18.      <div class="num">
  19.          <div>剩余</div>
  20.          <div class="js_giftPercent">'+circleData.percent+'%</div>
  21.      </div>
  22. </div>

css代码:

  1. .progress_wrap{
  2.                      position: relative;
  3.                      margin:0 0 0 .14rem;
  4.                      width:.92rem;height:.92rem;
  5.                      //little和more用来展示黄色和绿色的效果
  6.                      &.little{
  7.                      .under{
  8.                      .rightcircle,.leftcircle{
  9.                      border-top:$progress_border_under_little;
  10.                      }
  11.                      .rightcircle{
  12.                      border-right:$progress_border_under_little;
  13.                      }
  14.                      .leftcircle{
  15.                      border-left:$progress_border_under_little;
  16.                      }
  17.                      }
  18.                      .up{
  19.                      .rightcircle,.leftcircle{
  20.                      border-top:$progress_border_up_little;
  21.                      }
  22.                      .rightcircle{
  23.                      border-right:$progress_border_up_little;
  24.                      }
  25.                      .leftcircle{
  26.                      border-left:$progress_border_up_little;
  27.                      }
  28.                      }
  29.                      //用遮挡实现左侧剩余百分比,遮住超出环形范围部分;核心是使用同心圆进行边框进行遮挡
  30.                      .up_left_cover{
  31.                      width:.47rem;height:.92rem;
  32.                      .leftcircle{
  33.                      top:-.02rem;
  34.                      width:.74rem;height:.74rem;
  35.                      border:.11rem solid transparent;
  36.                      border-top:$progress_border_up_left_cover_little;
  37.                      border-left:$progress_border_up_left_cover_little;
  38.                      //实际值为195deg,被遮挡环颜色值深有光晕,需要将角度进行微调(-191deg)进行完全遮挡
  39.                      -webkit-transform:rotate(-191deg);
  40.                      }
  41.                      }
  42.                      }
  43.                      &.more{
  44.                      .under{
  45.                      .rightcircle,.leftcircle{
  46.                      border-top:$progress_border_under;
  47.                      }
  48.                      .rightcircle{
  49.                      border-right:$progress_border_under;
  50.                      }
  51.                      .leftcircle{
  52.          border-left:$progress_border_under;
  53.                      }
  54.                      }
  55.                      .up{
  56.                      .rightcircle,.leftcircle{
  57.                      border-top:$progress_border_up;
  58.                      }
  59.                      .rightcircle{
  60.                      border-right:$progress_border_up;
  61.                      }
  62.                      .leftcircle{
  63.                      border-left:$progress_border_up;
  64.                      }
  65.                      }
  66.                      }
  67.                      .right,.left{
  68.                      position: absolute;top:0;overflow:hidden;
  69.                      width:.46rem;height:.92rem;
  70.                      .circleProgress{
  71.                      position: absolute; top:0;
  72.                      width: .78rem; height: .78rem;
  73.                      border:.07rem solid transparent; border-radius: 50%;
  74.                      }
  75.                      .rightcircle{
  76.                      right:0;
  77.                      -webkit-transform: rotate(15deg);
  78.                      }
  79.                      .leftcircle{
  80.                      left:0;
  81.                      -webkit-transform: rotate(-15deg);
  82.                      }
  83.                      }
  84.                      .right{
  85.                      right:0;
  86.                      }
  87.                      .left{
  88.                      left:0;
  89.                      }
  90.                      .num{
  91.                      position: absolute;left:50%;top:50%;
  92.                      width:.5rem;
  93.                      transform:translate(-50%,-50%);
  94.                      font-size:.12rem;color:$public_auxiliary_col;text-align:center;line-height:.26rem;
  95.                      }
  96.                  }

js代码:

  1. function giftCircleProgressFn(per){
  2.              var circleData = {};
  3.              var percent = parseInt(per);
  4.              //领取进度环形颜色className
  5.              var halfClassName = percent<50?"little":"more";
  6.              //左半环遮罩层显示样式状态
  7.              var leftCircleDisplay = percent<50?"block":"none";
  8.              var leftRotate = 0;
  9.              var rightRotate = 0;
  10.              //以50%为界限;<50%:右半圆占比为0,左半圆需要使用遮罩进行遮挡,展示剩余部分
  11.              // >50%:左半圆占比100%,右半圆直接使用百分比计算所占部分即可
  12.              //注意:在半圆中计算百分比时,要将百分比乘以2。
  13.              if(percent<50){
  14.                  leftRotate = -15-180+150*(percent*2)/100;
  15.                  rightRotate = -135;
  16.              }else{
  17.                  leftRotate = -15;
  18.                  rightRotate = -135+(150*((percent-50)*2)/100); //比例在半环计算需要*2倍
  19.              }
  20.              circleData = {
  21.                  leftRotate:leftRotate, //左半环进度
  22.                  rightRotate:rightRotate, //右半环进度
  23.                  halfClassName:halfClassName, //50% 进度环 变色
  24.                  leftCircleDisplay:leftCircleDisplay, //左半环遮罩
  25.                  percent:per //进度百分比
  26.              }
  27.              return circleData
  28.          }

 环形旋转角度换算需要根据不同需求进行微调即可。
 

总结

以上所述是小编给大家介绍的使用CSS3实现环形进度条效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

标签: 使用 CSS 实现 进度条 效果