Javascript 将进度条值添加到进度条元素的中心
我有一些基于rest服务api动态生成的进度条。 如何在进度条的中心添加值,就像视频中使用纯CSS和JS(没有jquery或其他库)一样 代码Javascript 将进度条值添加到进度条元素的中心,javascript,css,progress-bar,Javascript,Css,Progress Bar,我有一些基于rest服务api动态生成的进度条。 如何在进度条的中心添加值,就像视频中使用纯CSS和JS(没有jquery或其他库)一样 代码 Optus前端分配 .包装纸{ 宽度:500px; } .进度条{ 背景色:#eee; 边界半径:5px; 盒影:0.2×3×rgba(0,0,0,0.25)插页; 位置:相对位置; } .进度条填充{ 显示:块; 高度:22px; 背景色:#659cef; 边界半径:3px; 过渡:宽度500ms,易于进出; } var xmlhttp=new
Optus前端分配
.包装纸{
宽度:500px;
}
.进度条{
背景色:#eee;
边界半径:5px;
盒影:0.2×3×rgba(0,0,0,0.25)插页;
位置:相对位置;
}
.进度条填充{
显示:块;
高度:22px;
背景色:#659cef;
边界半径:3px;
过渡:宽度500ms,易于进出;
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
//使用parse()方法将JSON字符串转换为JSON对象
var responseJsonObj=JSON.parse(this.responseText);
//console.log(responseJsonObj);
对于(设p=0;p
预期产量
这使用CSS网格使进度条和值都位于网格的同一单元格中,因此它们重叠而没有任何奇怪的定位 注释在代码中,用于帮助进度条的某些样式
<html><head>
<style>
.bar-container {
position: relative;
padding: 5px;
height: 40px;
width: 400px;
}
.prog-bar {
height: 100%;
width: 100%;
appearance: none;
}
.prog-label {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
</body>
<script>
const apiData = {"buttons":[5,47,-45,-22],"bars":[66,48,49,65],"limit":160};
function progValue(container, val){
const bar = container.getElementsByClassName('prog-bar')[0];
const label = container.getElementsByClassName('prog-label')[0];
const max = parseInt(bar.getAttribute('max'));
const oldVal = parseInt(bar.getAttribute('value'));
const newVal = Math.min(Math.max(oldVal + val, 0), max);
bar.setAttribute('value', newVal);
const percent = (newVal / max) * 100;
label.innerHTML = percent.toFixed(0) + '%';
}
for (let i = 0; i < apiData.bars.length; i++) {
const val = apiData.bars[i];
const container = document.createElement('div');
container.setAttribute('class', 'bar-container');
container.setAttribute('id', 'Progress' + i);
const progress = document.createElement('progress');
progress.setAttribute('class', 'prog-bar');
progress.setAttribute('value', 0);
progress.setAttribute('max', apiData.limit);
const label = document.createElement('label');
label.setAttribute('class', 'prog-label');
container.appendChild(progress);
container.appendChild(label);
document.body.appendChild(container);
progValue(container, val);
}
const select = document.createElement('select');
select.setAttribute('id', 'barSelector');
for (let bar of document.getElementsByClassName('bar-container')) {
const opt = document.createElement('option');
opt.text = bar.getAttribute('id');
opt.innerHTML = bar.getAttribute('id');
select.appendChild(opt);
}
document.body.appendChild(select);
for(let butVal of apiData.buttons) {
const button = document.createElement('button');
button.innerHTML = butVal;
button.addEventListener('click', event => {
const select = document.getElementById('barSelector');
const val = select.value;
const container = document.getElementById(val);
if(container) {
progValue(container, butVal);
}
});
document.body.appendChild(button);
}
</script>
</html>
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
//使用parse()方法将JSON字符串转换为JSON对象
var responseJsonObj=JSON.parse(this.responseText);
//console.log(responseJsonObj);
对于(设p=0;p<html><head>
<style>
.bar-container {
position: relative;
padding: 5px;
height: 40px;
width: 400px;
}
.prog-bar {
height: 100%;
width: 100%;
appearance: none;
}
.prog-label {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
</body>
<script>
const apiData = {"buttons":[5,47,-45,-22],"bars":[66,48,49,65],"limit":160};
function progValue(container, val){
const bar = container.getElementsByClassName('prog-bar')[0];
const label = container.getElementsByClassName('prog-label')[0];
const max = parseInt(bar.getAttribute('max'));
const oldVal = parseInt(bar.getAttribute('value'));
const newVal = Math.min(Math.max(oldVal + val, 0), max);
bar.setAttribute('value', newVal);
const percent = (newVal / max) * 100;
label.innerHTML = percent.toFixed(0) + '%';
}
for (let i = 0; i < apiData.bars.length; i++) {
const val = apiData.bars[i];
const container = document.createElement('div');
container.setAttribute('class', 'bar-container');
container.setAttribute('id', 'Progress' + i);
const progress = document.createElement('progress');
progress.setAttribute('class', 'prog-bar');
progress.setAttribute('value', 0);
progress.setAttribute('max', apiData.limit);
const label = document.createElement('label');
label.setAttribute('class', 'prog-label');
container.appendChild(progress);
container.appendChild(label);
document.body.appendChild(container);
progValue(container, val);
}
const select = document.createElement('select');
select.setAttribute('id', 'barSelector');
for (let bar of document.getElementsByClassName('bar-container')) {
const opt = document.createElement('option');
opt.text = bar.getAttribute('id');
opt.innerHTML = bar.getAttribute('id');
select.appendChild(opt);
}
document.body.appendChild(select);
for(let butVal of apiData.buttons) {
const button = document.createElement('button');
button.innerHTML = butVal;
button.addEventListener('click', event => {
const select = document.getElementById('barSelector');
const val = select.value;
const container = document.getElementById(val);
if(container) {
progValue(container, butVal);
}
});
document.body.appendChild(button);
}
</script>
</html>