有没有更有效的方法来制作多步动画?

时间:2018-06-23 00:20:20

标签: html css css3 css-transitions

我正在尝试进行与以下代码示例中的转换非常相似的转换。它在这里很好用,但是当我在实际网站上使用它时几乎无法使用。像此简化示例中一样,某些过渡延迟似乎被跳过并且不平滑。在该网站上,我试图将其作为幻灯片放映过渡的一部分,以便同时进行其他过渡,因此我认为GPU无法处理我的设置方法。还有另一种方法可以做到这一点,这样我就不会分别转换每个div了吗?我可以进行一个过渡直到上一个过渡开始之前开始吗?可能是关键帧或步骤?

function toggleActive(){
  var element = document.getElementById('wrap');
  element.classList.toggle('active');
}
#wrap{
position:relative;
overflow:hidden;
}
#wrap img{
  max-width:100%;
}
.diagonal-transition{
  background-color:#edf8fb;
  position: absolute;
  height: 201%;
  width:25%;
  transform: rotate(45deg);
  top: -200%;
  opacity:.7;
  transition-duration: .5s;
}
.diag-box-1{
  left:67%;
}
.diag-box-2{
  left:102.4%;
  transition-delay: .5s;
}
.diag-box-3{
  left:137.8%;
  transition-delay: 1s;
}
.diag-box-4{
  left:173.2%;
  transition-delay: 1.5s;
}
.diag-box-5{
  left:208.7%;
  transition-delay: 2s;
}
.active .diag-box-1, .active .diag-box-2, .active .diag-box-3, .active .diag-box-4, .active .diag-box-5{
 transform: rotate(45deg) translatey(100%);
}
<div id="wrap">
 <img src="https://s8.postimg.cc/3q4ybyfad/parrot.jpg">
 <div class="diagonal-transition diag-box-1"></div>
 <div class="diagonal-transition diag-box-2"></div>
 <div class="diagonal-transition diag-box-3"></div>
 <div class="diagonal-transition diag-box-4"></div>
 <div class="diagonal-transition diag-box-5"></div>
</div>
<button onclick="toggleActive()">ACTIVE STATE</button>

1 个答案:

答案 0 :(得分:1)

我只考虑背景和只考虑一个动画就可以做到这一点。然后,您可以调整background-size / background-position来控制动画的行为:

function toggleActive() {
  var element = document.getElementById('wrap');
  element.classList.toggle('active');
}
#wrap {
  position: relative;
  overflow: hidden;
  width: 500px;
  height: 300px;
  background-image: 
    linear-gradient(120deg,transparent 0% ,rgba(237, 248, 251, 0.7) 0%,rgba(237, 248, 251, 0.7) 20%,transparent 20.5%),
    linear-gradient(120deg,transparent 21%,rgba(237, 248, 251, 0.7) 20%, rgba(237, 248, 251, 0.7) 40%,transparent 40.5%),
    linear-gradient(120deg,transparent 41%,rgba(237, 248, 251, 0.7) 40%, rgba(237, 248, 251, 0.7) 60%,transparent 60.5%),
    linear-gradient(120deg,transparent 61%,rgba(237, 248, 251, 0.7) 60%, rgba(237, 248, 251, 0.7) 80%,transparent 80.5%),
    linear-gradient(120deg,transparent 81%,rgba(237, 248, 251, 0.7) 80%, rgba(237, 248, 251, 0.7) 100%,transparent 100%),
    url(https://s8.postimg.cc/3q4ybyfad/parrot.jpg); 
  background-position:0 0,0 0,0 0,0 0,0 0,center;
  background-size: 0 100%,0 100%,0 100%,0 100%,0 100%,cover;
  background-repeat: no-repeat;
}
.active {
 animation:change 1s linear forwards;
}
@keyframes change {
  0% {
     background-size: 100% 0,100% 0,100% 0,100% 0,100% 0,cover;
  }
  20% {
     background-size: 100% 100%,100% 0,100% 0,100% 0,100% 0,cover;
  }
  40% {
     background-size: 100% 100%,100% 100%,100% 0,100% 0,100% 0,cover;
  }
  60% {
     background-size: 100% 100%,100% 100%,100% 100%,100% 0,100% 0,cover;
  }
  80% {
     background-size: 100% 100%,100% 100%,100% 100%,100% 100%,100% 0,cover;
  }
  100% {
     background-size: 100% 100%,100% 100%,100% 100%,100% 100%,100% 100%,cover;
  }
}
<div id="wrap">
</div>
<button onclick="toggleActive()">ACTIVE STATE</button>