CSS动画:如何在每次迭代后应用变换?

时间:2017-06-01 10:35:15

标签: css animation css-animations

我试图在容器div中为4个div的列表设置动画。

我希望每一秒后都能将它们移动起来。

我定义了2个动画:scaleUp和slideUp。

@keyframes scaleUp {
 0%{
    transform: scale(1) translateY(0);
   }
100%{
    transform: scale(1.15) translateY(-90px);
   }
 }

@keyframes slideUp {
  0%{
    transform: translateY(0);
  }
  100%{
    transform: translateY(-90px);
  }
}

然后我以下列方式将动画应用于div:

.animated:nth-child(1){
  animation: slideUp 1s 4, scaleUp 1s ease;
}

.animated:nth-child(2){
  animation: slideUp 1s 4, scaleUp 1s ease 1s;
}

.animated:nth-child(3){
  animation: slideUp 1s 4, scaleUp 1s ease 2s;
}

.animated:nth-child(4){
  animation: slideUp 1s 4, scaleUp 1s ease 3s;
}

动画是我给div的类。

问题是,动画在每次迭代后重置,并且div在每次迭代后从其初始位置开始。

我在slideUp上尝试了animation-fill-mode: forwards,但只有在完成所有4次迭代后才会应用转换。如何在每次迭代后应用转换?

P.S。 You can view the code here

1 个答案:

答案 0 :(得分:0)

你需要让你的幻灯片动画每帧超过4个关键帧-90像素,然后根据动画时的位置为每个盒子设置一个单独的放大动画:

styles.css
/*.center{
  text-align: center;
  vertical-align: middle;
}*/

.container{
  /*border: 1px solid black;*/
  margin-top: 50px;
  padding-top: 100px;
  overflow: hidden;
}

.card {
  /*border: 1px solid black;*/
  padding: 20px 10px;
  margin: 20px auto;
  background-color: #fff;
  box-shadow: 1px 3px 4px 1px rgba(5,5,5,0.14);
  width: 300px;
  height: 50px;
  text-align: center;
}

/*.active{
  transform: scale(1.2);
}*/

@keyframes scaleUp {
  0%{
    transform: scale(1) translateY(0);
  }
  100%{
    transform: scale(1.15) translateY(-90px);
  }
}

@keyframes scaleUp1 {
  0%{
    transform: scale(1) translateY(-90px);
  }
  100%{
    transform: scale(1.15) translateY(-180px);
  }
}

@keyframes scaleUp2 {
  0%{
    transform: scale(1) translateY(-180px);
  }
  100%{
    transform: scale(1.15) translateY(-270px);
  }
}

@keyframes scaleUp3 {
  0%{
    transform: scale(1) translateY(-270px);
  }
  100%{
    transform: scale(1.15) translateY(-360px);
  }
}

@keyframes slideUp {
  0%{
    transform: translateY(0);
  }
  25%{
    transform: translateY(-90px);
  }
  50%{
    transform: translateY(-180px);
  }
  75%{
    transform: translateY(-270px);
  }
  100%{
    transform: translateY(-360px);
  }
}

.animated:nth-child(1){
  animation: slideUp 4s, scaleUp 1s ease;
  animation-play-state: running;
  /*animation-iteration-count: infinite;*/
}
.animated:nth-child(2){
  animation: slideUp 4s, scaleUp1 1s ease 1s;
  animation-play-state: running;
  /*animation-iteration-count: infinite;*/
}
.animated:nth-child(3){
  animation: slideUp 4s, scaleUp2 1s ease 2s;
  animation-play-state: running;
  /*animation-iteration-count: infinite;*/
}
.animated:nth-child(4){
  animation: slideUp 4s, scaleUp3 1s ease 3s;
  animation-play-state: running;
  /*animation-iteration-count: infinite;*/
}