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

Posted

技术标签:

【中文标题】CSS / jQuery:动态更新的文本出现在右侧而不是圆形进度条内【英文标题】:CSS / jQuery: Dynamically updated text appears right of instead of inside circular progress bar 【发布时间】:2020-11-01 08:13:30 【问题描述】:

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

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

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

参考资料:

https://www.jqueryscript.net/other/Animated-Circular-Progress-Bar-with-jQuery-Canvas-Circle-Progress.html https://www.jqueryscript.net/demo/Animated-Circular-Progress-Bar-with-jQuery-Canvas-Circle-Progress/

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'
        
    );
);

非常感谢, 汤姆

【问题讨论】:

【参考方案1】:

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

<div class="first_circle">

代替:

<div class="first circle">

因此,为了您的工作方式,您要么需要修改 CSS,要么只需分离类。

这是一个添加了适当 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>

【讨论】:

我选择了这个解决方案,因为这正是我遇到的问题,并且它以这种方式完美运行。【参考方案2】:

/* 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 / jQuery:动态更新的文本出现在右侧而不是圆形进度条内的主要内容,如果未能解决你的问题,请参考以下文章

动态更新 Extjs autoEl :'button' 文本和 css

jQuery实现列表内容的动态载入特效

JQuery Mobile - 解决动态更新页面内容,CSS失效问题!

CSS:图像和文本在一行右侧,没有流文本?

文本框中的透明文本(css、js、jquery)

css 图像在文本字段的右侧部分