svg環形百分比進度條

螞蟻小編發表於2017-02-01
分享一段程式碼例項,它實現了環形百分比進度條效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  box-sizing: border-box;
}
 
html,
body {
  margin: 0 0;
  padding: 0;
  height: 100%;
}
 
body {
  -webkit-font-smoothing: antialiased;
  background: #212121;
  font-family: 'Roboto', Helvetica, Arial, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}
 
.donut {
  position: relative;
  width: 240px;
  height: 240px;
}
 
.donut__copy {
  text-align: center;
  width: 100%;
  height: 100%;
  padding-top: 68px;
  top: 0;
  left: 0;
  position: absolute;
}
 
.donut__title,
.donut__secondary {
  display: block;
  margin: 0;
  padding: 0;
}
 
.donut__title,
.donut__spic {
  color: #8a6fd5;
  font-weight: 200;
}
 
.donut__title {
  font-size: 79px;
  position: relative;
  animation: donutTitleFadeLeft 800ms 200ms cubic-bezier(0.99, 0.01, 0.22, 0.94) forwards;
  opacity: 0;
  transform: translateX(0);
}
 
.donut__spic {
  position: absolute;
  top: 20px;
  font-size: 30px;
  line-height: 1em;
  content: "%";
  animation: donutTitleFadeRight 800ms 200ms cubic-bezier(0.99, 0.01, 0.22, 0.94) forwards;
  opacity: 0;
  transform: translateY(-20px);
}
 
@keyframes donutTitleFadeLeft {
  from {
    opacity: 0;
    transform: translateX(0);
  }
  to {
    opacity: 1;
    transform: translateX(-10px);
  }
}
@keyframes donutTitleFadeRight {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
.donut__svg {
  transform: rotate(-90deg);
}
 
.donut__svg__circle--one {
  stroke-dasharray: 565.48668;
  stroke-dashoffset: 565;
}
 
.donut__svg__circle--one {
  animation: donut-show-one 1200ms cubic-bezier(0.99, 0.01, 0.62, 0.94) 0.5s forwards;
  transition: stroke-dasharray 400ms ease-in-out;
}
 
@keyframes donut-show-one {
  to {
    stroke-dashoffset: 101;
  }
}
.info {
  font-size: 14px;
  color: #999;
  position: absolute;
  flex: 1;
  bottom: 40px;
  width: 100%;
  left: 0;
  text-align: center;
}
.info a {
  color: #999;
}
 
  </style>
</head>
<body>
<div class="donut">
  <svg width="240" height="240" xmlns="http://www.w3.org/2000/svg" class="donut__svg">
    <circle id="donut-graph-x"
            class="donut__svg__scrim"
            r="90" cy="120" cx="120"
            stroke-width="3"
            stroke="#333" fill="none" />
    <circle id="donut-graph"
            class="donut__svg__circle--one"
            r="90" cy="120" cx="120"
            stroke-width="4" stroke="url(#purple)"
            stroke-linejoin="round"
            stroke-linecap="round" fill="none" />
    <defs>
      <linearGradient id="purple" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#7a5bcf" />
        <stop offset="100%" stop-color="#8A6FD5" />
      </linearGradient>
    </defs>
  </svg>
  <div class="donut__copy">
    <span class="donut__title">82<span class="donut__spic">%</span></span>
  </div>
</div>
</body>
</html>

相關文章