为什么我的自定义动画无法正常工作?

时间:2018-04-30 18:00:58

标签: html css html5 css3 css-animations

我的自定义@keyframes动画无法正常运行。

旋转时Block 1Block 2之间没有差距,但Block 2Block 3Block 3和{{1}之间的差距很小},以及Block 4Block 4

我无法理解为什么上面列出的块之间存在差距,或者为什么块1和块2之间没有阻塞。我应该怎么做才能消除间隙,我希望它看起来像没有间隙的盒子。

对此代码的任何其他建议也表示赞赏。 以下是代码。

Block 1
.main {
  width: 250px;
  height: 100px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.main>div {
  width: 250px;
  height: 100px;
  position: absolute;
  text-align: center;
  line-height: 100px;
  color: #fff;
  font-weight: bold;
  font-family: monospace;
  font-size: 2em;
  transition: 1s;
  cursor: pointer;
}

.child1 {
  background: red;
  transform-origin: bottom;
  animation: animate1 8s infinite;
}

.child2 {
  background: blue;
  transform-origin: top;
  animation: animate2 8s infinite;
}

.child3 {
  background: yellow;
  transform-origin: bottom;
  animation: animate3 8s infinite;
}

.child4 {
  background: green;
  transform-origin: top;
  animation: animate4 8s infinite;
}


/*.main:hover .child1{
	transform: translateY(-100%) rotateX(90deg);
}

.main:hover .child2{
	transform: translateY(0%) rotateX(0deg);
}*/

@keyframes animate1 {
  0% {
    transform: translateY(0%) rotateX(0deg);
  }
  25% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  50% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  75% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
  100% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
}

@keyframes animate2 {
  0% {
    transform: translateY(100%) rotateX(90deg);
  }
  25% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
  50% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  75% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  100% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
}

@keyframes animate3 {
  0% {
    transform: translateY(-100%) rotateX(90deg);
  }
  25% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
  50% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
  75% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  100% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
}

@keyframes animate4 {
  0% {
    transform: translateY(-100%) rotateX(90deg);
  }
  25% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  50% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
  75% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
  100% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
}

1 个答案:

答案 0 :(得分:4)

这是因为您要在状态之间设置transform-origin动画,因此您应该通过添加更多状态来避免这种情况,以便快速更改transform-origin。通过更改transform-origin,您可以在逻辑上更改元素的旋转方式,并创建不必要的移动,从而创建您看到的间隙。

为什么你在第1区和第2区之间没有看到它?

只是因为如果你看两个元素的第一个状态,你会看到变换原点没有变化,因此没有任何移动会产生与其他状态不同的差距:



.main {
  width: 250px;
  height: 100px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.main>div {
  width: 250px;
  height: 100px;
  position: absolute;
  text-align: center;
  line-height: 100px;
  color: #fff;
  font-weight: bold;
  font-family: monospace;
  font-size: 2em;
  transition: 1s;
  cursor: pointer;
}

.child1 {
  background: red;
  transform-origin: bottom;
  animation: animate1 8s infinite;
}

.child2 {
  background: blue;
  transform-origin: top;
  animation: animate2 8s infinite;
}

.child3 {
  background: yellow;
  transform-origin: bottom;
  animation: animate3 8s infinite;
}

.child4 {
  background: green;
  transform-origin: top;
  animation: animate4 8s infinite;
}

@keyframes animate1 {
  0% {
    transform-origin: bottom;
    transform: translateY(0%) rotateX(0deg);
  }
  25% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  50% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  75% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
  100% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
}

@keyframes animate2 {
  0% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
  25% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
  25.5% {
    transform-origin: bottom;
    transform: translateY(0%) rotateX(0deg);
  }
  50% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  75% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  75.5% {
    transform-origin: top;
    transform: translateY(-100%) rotateX(90deg);
  }
  100% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
}

@keyframes animate3 {
  0% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  0.5% {
    transform-origin: top;
    transform: translateY(-100%) rotateX(90deg);
  }
  25% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
  50% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
  50.5% {
    transform-origin: bottom;
    transform: translateY(0%) rotateX(0deg);
  }
  75% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  100% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
}

@keyframes animate4 {
  0% {
    transform-origin: top;
    transform: translateY(-100%) rotateX(90deg);
  }
  0.5% {
    transform-origin: top;
    transform: translateY(-100%) rotateX(90deg);
  }
  25% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
  25.5% {
    transform-origin: top;
    transform: translateY(-100%) rotateX(90deg);
  }
  50% {
    transform-origin: top;
    transform: translateY(100%) rotateX(90deg);
  }
  75% {
    transform-origin: top;
    transform: translateY(0%) rotateX(0deg);
  }
  75.5% {
    transform-origin: bottom;
    transform: translateY(0%) rotateX(0deg);
  }
  100% {
    transform-origin: bottom;
    transform: translateY(-100%) rotateX(90deg);
  }
}

<div class="main">
  <div class="child1">Block 1</div>
  <div class="child2">Block 2!</div>
  <div class="child3">Block 3</div>
  <div class="child4">Block 4</div>
</div>
&#13;
&#13;
&#13;