svg圆形进度条

css3实现的圆形进度条较复杂,代码量较多,本文稍微讲解下如何使用svg实现圆形进度条。

  • svg实现一个圆用<circle>元素:
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="50" />
</svg>

以上代码绘制一个半径为50的圆。

  • 圆形进度条主要需要两个属性stroke-dasharraystroke-dashoffset
    • stroke-dasharray用于绘制形状轮廓的虚线和间隙。
	<svg viewBox="0 0 30 12" xmlns="http://www.w3.org/2000/svg">
  <!-- No dashes nor gaps -->
  <line x1="0" y1="1" x2="30" y2="1" stroke="black" />

  <!-- Dashes and gaps of the same size -->
  <line x1="0" y1="3" x2="30" y2="3" stroke="black" stroke-dasharray="4" />

  <!-- Dashes and gaps of different sizes -->
  <line x1="0" y1="5" x2="30" y2="5" stroke="black" stroke-dasharray="4 1" />

  <!-- Dashes and gaps of various sizes with an odd number of values -->
  <line x1="0" y1="7" x2="30" y2="7" stroke="black" stroke-dasharray="4 1 2" />

  <!-- Dashes and gaps of various sizes with an even number of values -->
  <line
    x1="0"
    y1="9"
    x2="30"
    y2="9"
    stroke="black"
    stroke-dasharray="4 1 2 3" />

  <!-- Dashes starting with a gap -->
  <line
    x1="0"
    y1="11"
    x2="30"
    y2="11"
    stroke="black"
    stroke-dasharray="0 4 0" />
</svg>
以上是`stroke-dasharray`的使用, 效果如下图所示:

stroke-dasharray

  • stroke-dashoffset 定义关联的破折号阵列的呈现上的偏移。
<svg viewBox="-3 0 33 10" xmlns="http://www.w3.org/2000/svg">
  <!-- No dash array -->
  <line x1="0" y1="1" x2="30" y2="1" stroke="black" />

  <!-- No dash offset -->
  <line x1="0" y1="3" x2="30" y2="3" stroke="black" stroke-dasharray="3 1" />

  <!--
  The start of the dash array computation
  is pulled by 3 user units
  -->
  <line
    x1="0"
    y1="5"
    x2="30"
    y2="5"
    stroke="black"
    stroke-dasharray="3 1"
    stroke-dashoffset="3" />

  <!--
  The start of the dash array computation
  is pushed by 3 user units
  -->
  <line
    x1="0"
    y1="7"
    x2="30"
    y2="7"
    stroke="black"
    stroke-dasharray="3 1"
    stroke-dashoffset="-3" />

  <!--
  The start of the dash array computation
  is pulled by 1 user units which ends up
  in the same rendering as the previous example
  -->
  <line
    x1="0"
    y1="9"
    x2="30"
    y2="9"
    stroke="black"
    stroke-dasharray="3 1"
    stroke-dashoffset="1" />

  <!--
  the following red lines highlight the
  offset of the dash array for each line
  -->
  <path d="M0,5 h-3 M0,7 h3 M0,9 h-1" stroke="rgba(255,0,0,.5)" />
</svg>

以上是stroke-dashoffset的使用,效果如下图所示:
在这里插入图片描述

  • 了解了svg圆形进度条需使用的属性后,实现:
<svg viewBox="0 0 100 100">
      <circle cx='50' cy="50" r="20"
        stroke-linecap="round"
        stroke-dasharray="125.6"
        stroke-dashoffset="40"
        stroke-width="4"
        stroke="#d7b386"
      />
</svg>

效果如图所示:
圆弧

  • 需要关注两点:
    1. stroke-dasharray=Math.PI*r*2(r为半径)即直接画一个圆周。
    2. stroke-dashoffset=减去的弧度即定义偏移量。

转载CSS3实现的圆形进度条:https://www.xiabingbao.com/css/2015/07/27/css3-animation-circle.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值