jQuery animate()宽度持续时间

时间:2017-10-04 10:26:54

标签: jquery twitter-bootstrap jquery-animate

我试图设置进度条的宽度,但无论我使用动画的持续时间是相同的。在Chrome中它只是延迟。在Firefox中,动画在开始时很慢而在en中更快。我不知道为什么会这样。



var $progressBar = $(".progress-bar")
$progressBar.animate({
  width: $progressBar.text()
}, 5000);

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-xs-12 col-sm-12">
  <div class="progress">
    <div class="progress-bar progress-bar-striped progress-bar-animated" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width:0%">60%</div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

问题是因为默认情况下,Bootstrap在transition: width元素的CSS上应用.progress-bar规则。这会干扰动画条形宽度的jQuery逻辑,因此您需要覆盖该CSS规则以将其删除,如下所示:

var $progressBar = $(".progress-bar");
$progressBar.animate({
  width: $progressBar.text()
}, 5000);
div .progress-bar {
  transition: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-xs-12 col-sm-12">
  <div class="progress">
    <div class="progress-bar progress-bar-striped progress-bar-animated" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width:0%">60%</div>
  </div>
</div>