将每个进度条从0加载到多个进度条动画中的值

时间:2017-07-17 15:12:46

标签: javascript html css

我正在尝试在分组进度条中添加动画,将每个进度条从0加载到其值。例如,在我的示例代码中,我想先加载红色进度条,然后加载绿色进度条。我怎么能这样做?

请检查this jsfiddle中的代码。

HTML:

<div class="progress-bar-outer">
  <div class="progress-bar-inner">
  </div>
  <div class="progress-bar-inner2">
  </div>
</div>

的CSS:

.progress-bar-outer {
  width: 300px;
  height: 40px;
  flex: auto;
  display: flex;
  flex-direction: row;
  border-radius: 0.5em;
  overflow: hidden;
  background-color: gray;
}
.progress-bar-inner {
  /* You can change the `width` to change the amount of progress. */
  width: 75%;
  height: 100%;
  background-color: red;
}
.progress-bar-inner2 {
  /* You can change the `width` to change the amount of progress. */
  width: 50%;
  height: 100%;
  background-color: green;
}

.progress-bar-outer div {
      animation:loadbar 3s;
    -webkit-animation:loadbar 3s;
}

@keyframes loadbar {
    0% {width: 0%;left:0;right:0}
}

3 个答案:

答案 0 :(得分:0)

I would transition transform instead for better performance. Use translateX(-100%) with opacity: 0 to move them to their default, hidden position, then animate to translateX(0); opacity: 1; to put them in place. And just add an animation-delay to the green bar that matches the animation-duration

I made the bars semi-opaque to show when the animations fire.

.progress-bar-outer {
  width: 300px;
  height: 40px;
  border-radius: 0.5em;
  overflow: hidden;
  background-color: gray;
  display: flex;
}

.progress-bar-inner {
  /* You can change the `width` to change the amount of progress. */
  width: 75%;
  background-color: red;
}

.progress-bar-inner2 {
  /* You can change the `width` to change the amount of progress. */
  width: 100%;
  background-color: green;
}

.progress-bar-outer div {
  transform: translateX(-100%);
  animation: loadbar 3s forwards;
  -webkit-animation: loadbar 3s forwards;
  opacity: 0;
}

.progress-bar-outer .progress-bar-inner2 {
  animation-delay: 3s;
}

@keyframes loadbar {
  0% {
    opacity: 1;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/progress -->

<div class="progress-bar-outer">
  <div class="progress-bar-inner">
  </div>
  <div class="progress-bar-inner2">
  </div>
</div>

答案 1 :(得分:0)

修改Michael Coker的答案,以更好地反映我对你所要求的解释。

.progress-bar-outer {
  width: 300px;
  height: 40px;
  border-radius: 0.5em;
  overflow: hidden;
  background-color: gray;
  position: relative;
}

.progress-bar-inner {
  /* You can change the `width` to change the amount of progress. */
  width: 100%;
  background-color: red;
  z-index: 1;
}

.progress-bar-inner2 {
  /* You can change the `width` to change the amount of progress. */
  width: 100%;
  background-color: green;
  z-index: 2;
}

.progress-bar-outer div {
  position: absolute;
  top: 0; bottom: 0;
  transform: translateX(-100%);
  animation: loadbar 3s linear;
  -webkit-animation: loadbar 3s linear;
  opacity: 1;
}

.progress-bar-outer .progress-bar-inner2 {
  animation-delay: 3s;
}

@keyframes loadbar {
  100% {
    transform: translateX(0);
  }
}
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/progress -->

<div class="progress-bar-outer">
  <div class="progress-bar-inner">
  </div>
  <div class="progress-bar-inner2">
  </div>
</div>

答案 2 :(得分:0)

将Transition应用于内部类,向辅助内部添加延迟,并使用不透明度在转换开始之前隐藏元素。

.progress-bar-inner {
  animation:loadbar 2s;
  -webkit-animation:loadbar 2s;
}
.progress-bar-inner2 {
  -webkit-animation: loadbar 2s ease 2s forwards;
  animation: loadbar 2s ease 2s forwards
  animation-delay: 2s;
  -webkit-animation-delay: 2s;
  opacity:0;
}

@keyframes loadbar {
    0% { width: 0%;left:0;right:0}
    1% { opacity: 1}
    100% { opacity: 1}
}

参见工作示例: https://jsfiddle.net/dfkLexuv/10/