CSS / jQuery: 动态更新的文本显示在右侧而不是圆形进度条内

CSS / jQuery: Dynamically updated text appears right of instead of inside circular progress bar

我正在尝试实现带有动画的圆形进度条,并从以下资源中找到了该插件。

我下载并包含了该插件,并使用了演示页面中的源代码(HTML 和 JS),它可以正常工作。
但是,我的问题是 动画文本,即通过 JS 生成的值(从 0 到设置的百分比) 直接出现在圆圈/图表中,而不是在它们里面(就像在演示中一样)。

我假设我在这里遗漏了一些 CSS 但我不确定我需要添加什么来移动圆圈/图表内的值。源码里的CSS有注释说这里不需要
有人可以帮我解决这个问题吗?

参考文献:

HTML:

<h1 style="margin-top:150px;">jQuery Circle Progress Demos</h1>
<div class="circles">
    <div class="first_circle">
        <span>no <br/> animation</span>
    </div>
    <div class="second_circle">
        <strong>0</strong>  <!-- This should appear inside the circle when being updated via JS -->
        <span>animation <br/> progress</span>
    </div>
    <div class="third_circle">
        <strong>0</strong>  <!-- This should appear inside the circle when being updated via JS -->
        <span>value <br/> progress</span>
    </div>
    <div class="forth_circle">
        <span>solid fill, <br/> custom angle</span>
    </div>
    <div class="fifth_circle">
        <span>image fill, <br/> custom sizes</span>
    </div>
</div>

JS:

$(document).ready(function() {  
    $('.first_circle').circleProgress({
        value: 0.35,
        animation: false,
        fill: { gradient: ['#ff1e41', '#ff5f43'] }
    });
    $('.second_circle').circleProgress({
        value: 0.6
    }).on('circle-animation-progress', function(event, progress) {
        $(this).find('strong').html(parseInt(100 * progress) + '<i>%</i>');
    });
    $('.third_circle').circleProgress({
        value: 0.8,
        fill: { gradient: ['#0681c4', '#07c6c1'] }
    }).on('circle-animation-progress', function(event, progress, stepValue) {
        $(this).find('strong').text(String(stepValue.toFixed(2)).substr(1));
    });
    $('.forth_circle').circleProgress({
        startAngle: -Math.PI / 4 * 3,
        value: .5,
        fill: { color: '#ffa500' }
    });
    $('.fifth_circle').circleProgress({
        value: 1,
        size: 60,
        thickness: 20,
        fill: {
            color: 'lime'
        }
    });
});

非常感谢,
汤姆

/* Examples */
(function($) {
  /*
   * Example 1:
   *
   * - no animation
   * - custom gradient
   *
   * By the way - you may specify more than 2 colors for the gradient
   */
  $('.first.circle').circleProgress({
    value: 0.35,
    animation: false,
    fill: {gradient: ['#ff1e41', '#ff5f43']}
  });

  /*
   * Example 2:
   *
   * - default gradient
   * - listening to `circle-animation-progress` event and display the animation progress: from 0 to 100%
   */
  $('.second.circle').circleProgress({
    value: 0.6
  }).on('circle-animation-progress', function(event, progress) {
    $(this).find('strong').html(Math.round(100 * progress) + '<i>%</i>');
  });

  /*
   * Example 3:
   *
   * - very custom gradient
   * - listening to `circle-animation-progress` event and display the dynamic change of the value: from 0 to 0.8
   */
  $('.third.circle').circleProgress({
    value: 0.75,
    fill: {gradient: [['#0681c4', .5], ['#4ac5f8', .5]], gradientAngle: Math.PI / 4}
  }).on('circle-animation-progress', function(event, progress, stepValue) {
    $(this).find('strong').text(stepValue.toFixed(2).substr(1));
  });

  /*
   * Example 4:
   *
   * - solid color fill
   * - custom start angle
   * - custom line cap
   * - dynamic value set
   */
  var c4 = $('.forth.circle');

  c4.circleProgress({
    startAngle: -Math.PI / 4 * 3,
    value: 0.5,
    lineCap: 'round',
    fill: {color: '#ffa500'}
  });

  // Let's emulate dynamic value update
  setTimeout(function() { c4.circleProgress('value', 0.7); }, 1000);
  setTimeout(function() { c4.circleProgress('value', 1.0); }, 1100);
  setTimeout(function() { c4.circleProgress('value', 0.5); }, 2100);

  /*
   * Example 5:
   *
   * - image fill; image should be squared; it will be stretched to SxS size, where S - size of the widget
   * - fallback color fill (when image is not loaded)
   * - custom widget size (default is 100px)
   * - custom circle thickness (default is 1/14 of the size)
   * - reverse drawing mode
   * - custom animation start value
   * - usage of "data-" attributes
   */
  $('.fifth.circle').circleProgress({
    value: 0.7
    // all other config options were taken from "data-" attributes
    // options passed in config object have higher priority than "data-" attributes
    // "data-" attributes are taken into account only on init (not on update/redraw)
    // "data-fill" (and other object options) should be in valid JSON format
  });
})(jQuery);
body {
  background-color: #444;
  padding-top: 40px;
  font: 15px/1.3 Arial, sans-serif;
  color: #fff;
  text-align: center;
}

a {
  color: orange;
}

.new-tab-link {
  padding-right: 14px;
  background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3ggXDSIzCeRHfQAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAA9SURBVBjTY2RAA/+XMvxHF2NkwAOwacCq4P9Shv8suFQzRiNsYUEXwKoJ2VhkNrIaJgYiAAs2N2BVRMirAD6JHi10MCdVAAAAAElFTkSuQmCC) no-repeat right center;
}

.page-title {
  font: 400 40px/1.5 Open Sans, sans-serif;
  text-align: center;
}

.circles {
  margin-bottom: -10px;
}

.circle {
  width: 100px;
  margin: 6px 6px 20px;
  display: inline-block;
  position: relative;
  text-align: center;
  line-height: 1.2;
}

.circle canvas {
  vertical-align: top;
}

.circle strong {
  position: absolute;
  top: 30px;
  left: 0;
  width: 100%;
  text-align: center;
  line-height: 40px;
  font-size: 30px;
}

.circle strong i {
  font-style: normal;
  font-size: 0.6em;
  font-weight: normal;
}

.circle span {
  display: block;
  color: #aaa;
  margin-top: 12px;
}

p {
  margin: 40px 0;
}
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="https://rawgit.com/kottenator/jquery-circle-progress/1.2.2/dist/circle-progress.js"></script>

<div class="circles">
    <div class="first circle">
      <span>no <br> animation</span>
    </div>

    <div class="second circle">
      <strong></strong>
      <span>animation <br> progress</span>
    </div>

    <div class="third circle">
      <strong></strong>
      <span>value <br> progress</span>
    </div>

    <div class="forth circle">
      <span>custom angle, <br> value update</span>
    </div>

    <div
      class="fifth circle"
      data-value="0.9"
      data-size="60"
      data-thickness="20"
      data-animation-start-value="1.0"
      data-fill="{
        &quot;color&quot;: &quot;rgba(0, 0, 0, .3)&quot;,
        &quot;image&quot;: &quot;http://i.imgur.com/pT0i89v.png&quot;
      }"
      data-reverse="true"
    >
      <span>image fill, <br> custom sizes</span>
    </div>
  </div>
  

我只是从演示中把它放在一起,我想你错过了一些东西或删除了一些东西,因为它会抛出错误。

两个问题。首先,您 缺少一些 CSS 来让演示正常工作。该演示链接到一个 page-styles.css 文件,其中包含一些 CSS 的圆圈。这就带来了第二个问题。即使您只是粘贴 CSS,它也不会“按原样”工作,因为 circle 需要是它自己的 class。在您的标记和代码中,您通过在 firstcircle 之间添加下划线来组合两个 classes:

<div class="first_circle">

而不是:

<div class="first circle">

因此,为了您的工作方式,您需要修改 CSS,或者只是将 class 分开。

这是一个添加了适当的 CSS 的示例(我只是从 page-styles.css 文件中复制了相关的 CSS,而不是整个文件):

$(document).ready(function() {
  $('.first.circle').circleProgress({
    value: 0.35,
    animation: false,
    fill: {
      gradient: ['#ff1e41', '#ff5f43']
    }
  });
  $('.second.circle').circleProgress({
    value: 0.6
  }).on('circle-animation-progress', function(event, progress) {
    $(this).find('strong').html(parseInt(100 * progress) + '<i>%</i>');
  });
  $('.third.circle').circleProgress({
    value: 0.8,
    fill: {
      gradient: ['#0681c4', '#07c6c1']
    }
  }).on('circle-animation-progress', function(event, progress, stepValue) {
    $(this).find('strong').text(String(stepValue.toFixed(2)).substr(1));
  });
  $('.forth.circle').circleProgress({
    startAngle: -Math.PI / 4 * 3,
    value: .5,
    fill: {
      color: '#ffa500'
    }
  });
  $('.fifth.circle').circleProgress({
    value: 1,
    size: 60,
    thickness: 20,
    fill: {
      color: 'lime'
    }
  });
});
.circle {
  width: 100px;
  margin: 6px 6px 20px;
  display: inline-block;
  position: relative;
  text-align: center;
  line-height: 1.2;
}

.circle canvas {
  vertical-align: top;
}

.circle strong {
  position: absolute;
  top: 30px;
  left: 0;
  width: 100%;
  text-align: center;
  line-height: 40px;
  font-size: 30px;
}

.circle strong i {
  font-style: normal;
  font-size: 0.6em;
  font-weight: normal;
}

.circle span {
  display: block;
  color: #aaa;
  margin-top: 12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://www.jqueryscript.net/demo/Animated-Circular-Progress-Bar-with-jQuery-Canvas-Circle-Progress/dist/circle-progress.js"></script>
<div class="circles">
  <div class="first circle">
    <span>no <br/> animation</span>
  </div>

  <div class="second circle">
    <strong></strong>
    <span>animation <br/> progress</span>
  </div>

  <div class="third circle">
    <strong></strong>
    <span>value <br/> progress</span>
  </div>

  <div class="forth circle">
    <span>solid fill, <br/> custom angle</span>
  </div>

  <div class="fifth circle">
    <span>image fill, <br/> custom sizes</span>
  </div>
</div>