英文:
How can I display Bootstrap progress bar numbers under the bar?
问题
我可以创建使用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:
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 -->
<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 -->
答案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('progress-bar');
var count = 0;
var values = document.getElementById('values');
var value = document.createElement('div');
value.innerHTML = "0";
value.style.width = (parseInt(elements[0].style.width.split('%')[0]) - 1.6) + "%"
values.appendChild(value);
for (var i = 0; i < elements.length; i++) {
count += parseInt(elements[i].style.width.split('%')[0]);
//elements[i].innerHTML = count;
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);
}
var value = document.createElement('div');
value.innerHTML = "100";
values.appendChild(value);
<!-- language: lang-html -->
<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>
<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>
<!-- 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: '100';
}
.progress.labels-out::after {
content: '0';
left: 0;
}
<!-- language: lang-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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论