如何创建圆形图像进度条

时间:2019-05-09 13:27:00

标签: javascript jquery html css

我想创建一个圆形进度条,将图像显示为当前状态。 因此有两层,一个是背景图像,另一个是在顶部的图像,它根据饼状图的百分比显示自己。 (请参见示例图片)

示例

example

1 个答案:

答案 0 :(得分:1)

这是饼图进度条的基本实现,其中显示了SVG-s和进度百分比。

$(document).ready(function() {
  repeatFunc(function() {
    progress("#progressBar");
  }, 100, 50);
});

function progress(bar, func) {
  var center = parseInt($(bar).children("svg").children("circle").attr("cx"));
  var r = parseInt(center - ($(bar).children("svg").children("path").attr("stroke-width") / 2));
  var i = parseInt($(bar).children(".progress-text").text().split("%")[0]);
  $(bar).children(".progress-text").text((i + 1) + "%");
  $(bar).children("svg").children("path").attr("d", describeArc(center, center, r, 0, (360 / 100 * (i + 1))));
  if (i + 1 == 100) {
    $(bar).children("svg").children("path").attr("d", describeArc(center, center, r, 0, 359));
    $(bar).children("svg").children("path").attr("stroke-linecap", "round")
    $(bar).children(".progress-text").text("100%")
  }
}

function repeatFunc(func, n, x) {
  for (var i = 0; i < n; i++) {
    setTimeout(func, x * i);
  }
}

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
  return {
    x: centerX + (radius * Math.cos(angleInRadians)),
    y: centerY + (radius * Math.sin(angleInRadians))
  };
}

function describeArc(x, y, radius, startAngle, endAngle) {
  var start = polarToCartesian(x, y, radius, endAngle);
  var end = polarToCartesian(x, y, radius, startAngle);
  var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
  var d = [
    "M", start.x, start.y,
    "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
  ].join(" ");
  return d;
}
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html,
body {
  background-color: #fff;
  font-family: sans-serif;
  height: 100%;
}

body {
  font-size: 16px;
  margin: 0px;
  overflow-x: hidden;
}

.center {
  border-radius: 50%;
  height: 250px;
  left: 50%;
  overflow: hidden;
  position: absolute;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  width: 250px;
}

.progress-bar {
  height: 250px;
  width: 250px;
}

.progress-bar .progress-text {
  color: #fff;
  /* color of progress text */
  font-size: 40px;
  /* font size of progress text */
  left: 50%;
  position: absolute;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="center">
  <div id="progressBar" class="progress-bar">
    <svg width="250" height="250">
        <circle cx="125" cy="125" r="125" stroke="none" stroke-width="0" fill="gray" />
        <path fill="none" stroke="green" stroke-width="125" stroke-linecap="none" />
      </svg>
    <div class="progress-text">0%</div>
  </div>
</div>

相关问题