我希望在2.5秒内将进度条的宽度设置为0%到70%。但是,下面的代码会在2.5秒延迟后立即将宽度更改为70%。我错过了什么?
$(".progress-bar").animate({
width: "70%"
}, 2500);
JSFiddle: http://jsfiddle.net/WEYKL/
答案 0 :(得分:35)
问题在于默认的Bootstrap过渡效果,它会动画width
属性的任何更新。
如果你通过压制相应的风格来关闭它,它将正常工作,例如:
.progress-bar {
-webkit-transition: none !important;
transition: none !important;
}
答案 1 :(得分:4)
如果使用引导程序进度条,则非常容易,
只添加atria aria-valuenow =" percent_required%"与班级进行划分"进度条"像这样:
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="57%" aria-valuemin="0" aria-valuemax="57">
接下来,在脚本上:
<script>
$(document).on('ready',function(){
$('.progress .progress-bar').css("width",function() {
return $(this).attr("aria-valuenow") + "%";
})
})
</script>
重新加载,开始!
答案 2 :(得分:3)
因此,通过CSS或jQuery调整转换效果更有意义。
.progress-bar {
-webkit-transition: width 2.5s ease;
transition: width 2.5s ease;
}
只需更改宽度值。
$(".progress-bar").css('width', '70%');
答案 3 :(得分:0)
您可以通过添加以下内容对其进行修复:
.progress .progress-bar {
transition: unset;
}
var delay = 500;
$(".progress-bar").each(function(i) {
$(this).delay(delay * i).animate({
width: $(this).attr('aria-valuenow') + '%'
}, delay);
$(this).prop('Counter', 0).animate({
Counter: $(this).text()
}, {
duration: delay,
// easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now) + '%');
}
});
});
.progress {
margin-bottom: 20px;
}
.progress-bar {
width: 0;
}
.bg-purple {
background-color: #825CD6 !important;
}
.progress .progress-bar {
transition: unset;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
<h2>Bootstrap 4 Progress Bars</h2>
<div class="progress border">
<div class="progress-bar progress-bar-striped progress-bar-animated bg-danger" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100">70</div>
</div>
<div class="progress border">
<div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100">50</div>
</div>
<div class="progress border">
<div class="progress-bar progress-bar-striped progress-bar-animated bg-purple" role="progressbar" aria-valuenow="90" aria-valuemin="0" aria-valuemax="100">90</div>
</div>
</div>