我正在尝试编写jQuery代码以根据进度条的宽度更新引导进度条的背景色。因此,例如,如果低于50%,则该条应为红色。如果介于50%到70%之间,进度条应为黄色;如果高于70%,则进度条应为绿色。我需要代码来获取进度栏的值,并检查这些范围并更改背景。
HTML
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:100%" aria-valuenow="100" aria-valuemin="0" aria-valumax="100" >100%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:80%" aria-valuenow="80" aria-valuemin="0" aria-valumax="100" >80%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:90%" aria-valuenow="90" aria-valuemin="0" aria-valumax="100" >90%</div></div></td>
jQuery
<script>
var value = $('.progress-bar').css('width');
$(document).ready(function(){
if(value < 50){
$('.progress-bar').css('background-color', 'red');
}
else if(value >= 50) && (value < 70){
$('.progress-bar').css('background-color', 'yellow');
}
});
</script>
<tr>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:98%" aria-valuenow="98" aria-valuemin="0" aria-valumax="100" >98%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:72%" aria-valuenow="72" aria-valuemin="0" aria-valumax="100" >72%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:82%" aria-valuenow="82" aria-valuemin="0" aria-valumax="100" >82%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:56%" aria-valuenow="56" aria-valuemin="0" aria-valumax="100" >56%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:75%" aria-valuenow="75" aria-valuemin="0" aria-valumax="100" >75%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:100%" aria-valuenow="100" aria-valuemin="0" aria-valumax="100" >100%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:30%" aria-valuenow="30" aria-valuemin="0" aria-valumax="100" >30%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:33%" aria-valuenow="33" aria-valuemin="0" aria-valumax="100" >33%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:77%" aria-valuenow="77" aria-valuemin="0" aria-valumax="100" >77%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:98%" aria-valuenow="98" aria-valuemin="0" aria-valumax="100" >98%</div></div></td>
<td><div class="progress"><div class="progress-bar" role="progressbar" style="width:98%" aria-valuenow="98" aria-valuemin="0" aria-valumax="100" >98%</div></div></td>
</tr>
答案 0 :(得分:0)
我很快完成了操作,因此出现了一些问题,例如最大酒吧大于100%。但是,您可以轻松地将其与自己的代码一起使用。 我使用了一个监听器,每次您增加Progressbar值时都会收到通知!
class Listener {
constructor(value){
this.value = value;
this.bar = $('.progress-bar');
}
notify(value){
$('#message').text(value);
if (value < 25){
this.bar.css('background-color', 'red');
}
else if (value < 50){
this.bar.css('background-color', 'blue');
}
else{
this.bar.css('background-color', 'yellow');
}
}
};
let listener = new Listener(0);
let timer = setInterval(function(){
let width = $('.progress-bar').width();
if (width >= 100) clearInterval(this);
let newValue = (($('.progress-bar').width() + 100) / $('.progress').width()) * 100;
$('.progress-bar').width($('.progress-bar').width() + 100);
listener.notify(newValue);
}, 1000);
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="progress">
<div id="progress" class="progress-bar" role="progressbar" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<div id="message"></div>
希望这对您有帮助!