你可以在进度条下方显示 Bootstrap 进度条的数字。

huangapple go评论77阅读模式
英文:

How can I display Bootstrap progress bar numbers under the bar?

问题

我可以创建使用Bootstrap的进度条

我希望这些数值显示在进度条下方而不是内部,类似于这样:

你可以在进度条下方显示 Bootstrap 进度条的数字。

我们如何实现这一点?我需要使用任何JavaScript图形库吗?

附注:我面临的主要挑战是在div下方显示数字,而不是使用颜色样式或计算这些数字。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<div class="container pt-4">
  <div class="progress">
    <div class="progress-bar" role="progressbar" style="width: 15%;">
      15
    </div>
    <div class="progress-bar bg-success" role="progressbar" style="width: 30%;">
      45
    </div>
    <div class="progress-bar bg-info" role="progressbar" style="width: 20%;">
      65
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>

<!-- end snippet -->
英文:

I am able to create progress bar with Bootstrap.

I want those numerical values to be displayed below the progress bar but not inside, something like this:

你可以在进度条下方显示 Bootstrap 进度条的数字。

How can we achieve this? Do I need to use any JavaScript graphing libraries?

PS: Note that the main challenge I am facing is to show the numbers below divs, and not with color styling or calculating those numbers.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;div class=&quot;container pt-4&quot;&gt;
  &lt;div class=&quot;progress&quot;&gt;
    &lt;div class=&quot;progress-bar&quot; role=&quot;progressbar&quot; style=&quot;width: 15%;&quot;&gt;
      15
    &lt;/div&gt;
    &lt;div class=&quot;progress-bar bg-success&quot; role=&quot;progressbar&quot; style=&quot;width: 30%;&quot;&gt;
      45
    &lt;/div&gt;
    &lt;div class=&quot;progress-bar bg-info&quot; role=&quot;progressbar&quot; style=&quot;width: 20%;&quot;&gt;
      65
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 2

以下是您提供的代码的翻译部分:

// 获取所有具有类名 'progress-bar' 的元素
var elements = document.getElementsByClassName('progress-bar');
var count = 0;
var values = document.getElementById('values');

// 创建一个新的 div 元素
var value = document.createElement('div');
value.innerHTML = "0";
value.style.width = (parseInt(elements[0].style.width.split('%')[0]) - 1.6) + "%";
values.appendChild(value);

// 遍历所有 'progress-bar' 元素
for (var i = 0; i < elements.length; i++) {
  count += parseInt(elements[i].style.width.split('%')[0]);
  
  // 创建一个新的 div 元素
  var value = document.createElement('div');
  value.innerHTML = count;

  if (i < elements.length - 1)
    value.style.width = parseInt(elements[i + 1].style.width.split('%')[0]) + "%";
  else
    value.style.width = (100 - count - 4) + "%";
  values.appendChild(value);
}

// 创建最后一个 div 元素
var value = document.createElement('div');
value.innerHTML = "100";
values.appendChild(value);
<!-- 引入 Bootstrap 样式表 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous" />

<div class="container">
  <div class="progress">
    <div class="progress-bar" role="progressbar" style="width: 15%;"></div>
    <div class="progress-bar bg-success" role="progressbar" style="width: 30%;"></div>
    <div class="progress-bar bg-info" role="progressbar" style="width: 20%;"></div>
  </div>
  <div id="values" style="display: flex; flex-flow: row wrap;"></div>
</div>

<!-- 引入 Bootstrap 脚本 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
英文:

One solution would be to add a div below yours and to use some JavaScript

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var elements = document.getElementsByClassName(&#39;progress-bar&#39;);
var count = 0;
var values = document.getElementById(&#39;values&#39;);
var value = document.createElement(&#39;div&#39;);
value.innerHTML = &quot;0&quot;;
value.style.width = (parseInt(elements[0].style.width.split(&#39;%&#39;)[0]) - 1.6) + &quot;%&quot;
values.appendChild(value);

for (var i = 0; i &lt; elements.length; i++) {
  count += parseInt(elements[i].style.width.split(&#39;%&#39;)[0]);
  //elements[i].innerHTML = count;

  var value = document.createElement(&#39;div&#39;);
  value.innerHTML = count;

  if (i &lt; elements.length - 1)
    value.style.width = parseInt(elements[i + 1].style.width.split(&#39;%&#39;)[0]) + &quot;%&quot;
  else
    value.style.width = (100 - count - 4) + &quot;%&quot;;
  values.appendChild(value);
}
var value = document.createElement(&#39;div&#39;);
value.innerHTML = &quot;100&quot;;
values.appendChild(value);

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot; /&gt;

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;progress&quot;&gt;
    &lt;div class=&quot;progress-bar&quot; role=&quot;progressbar&quot; style=&quot;width: 15%;&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;progress-bar bg-success&quot; role=&quot;progressbar&quot; style=&quot;width: 30%;&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;progress-bar bg-info&quot; role=&quot;progressbar&quot; style=&quot;width: 20%;&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div id=&quot;values&quot; style=&quot;display: flex; flex-flow: row wrap;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 1

以下是翻译好的内容:

一些CSS技巧和伪元素应该足够了请注意我使用`<span>`标签包裹了数值以进行定位通过为结构添加自定义类我们可以覆盖Bootstrap的样式而无需使用`!important`,这将使以后的工作更加轻松并避免样式化所有进度条

还请注意Bootstrap提供了有关位置溢出和边框半径的类但我将这些内容放在了CSS中以增加清晰度我在外部元素上使用了边框类

HTML部分如下

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    
    <div class="container pt-4">
      <div class="progress labels-out border border-2">
        <div class="progress-bar" role="progressbar" style="width: 15%;">
          <span>15</span>
        </div>
        <div class="progress-bar bg-success" role="progressbar" style="width: 30%;">
          <span>45</span>
        </div>
        <div class="progress-bar bg-info" role="progressbar" style="width: 20%;">
          <span>65</span>
        </div>
      </div>
    </div>
英文:

A little CSS whizbang and some pseudo-elements should do. Note that I wrapped the numeric values with spans for positioning. By giving the structure a custom class we can override Bootstrap's styles without using !important, which makes work easier down the road and avoids styling all progress bars.

Also note that Bootstrap provides classes for position, overflow, and border-radius, but I've put those things in the CSS for clarity. I did use border classes on the outer element.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.progress.labels-out,
.progress.labels-out .progress-bar {
  position: relative; /* allows positioning of child elements */
  overflow: visible; /* lets the child elements be seen */
  border-radius: 0;
}

.progress.labels-out .progress-bar span,
.progress.labels-out::before,
.progress.labels-out::after {
  position: absolute;
  top: calc(100% + 5px); /* shift down the height of the parent plus a bit */
  left: 100%;
  transform: translateX(-50%); /* shift left half its own width to center */
  color: #000;
  font-weight: bold;
}

.progress.labels-out::before {
  content: &#39;100&#39;;
}

.progress.labels-out::after {
  content: &#39;0&#39;;
  left: 0;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;div class=&quot;container pt-4&quot;&gt;
  &lt;div class=&quot;progress labels-out border border-2&quot;&gt;
    &lt;div class=&quot;progress-bar&quot; role=&quot;progressbar&quot; style=&quot;width: 15%;&quot;&gt;
      &lt;span&gt;15&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;progress-bar bg-success&quot; role=&quot;progressbar&quot; style=&quot;width: 30%;&quot;&gt;
      &lt;span&gt;45&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;progress-bar bg-info&quot; role=&quot;progressbar&quot; style=&quot;width: 20%;&quot;&gt;
      &lt;span&gt;65&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 21:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定