有没有更有效的方法来制作多步动画? [英] Is there a more efficient way to do multi-step animations?

查看:65
本文介绍了有没有更有效的方法来制作多步动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to make transitions very similar to what I have in the code example below. It works pretty well here, but when I use it on the real site it almost never works. Some transition-delays seem to be skipped and its not smooth like in this simplified example. On the site i'm trying to make this as part of a slideshow transition so other transitions are happening at the same time so i'm thinking the GPU can't handle the way I have it set up. Is there another way to accomplish this so that i'm not transitioning each div separately? Can I make one transition not start until the previous one has started? Possibly with keyframes or steps?

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>

推荐答案

我只考虑背景而做了不同的事情还有一个动画然后,您可以调整背景大小 / 背景位置来控制动画的行为:

I would do this differently considering only backgrounds and only one animation. Then you can adjust background-size/background-position to control the behavior of the animation:

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>

这篇关于有没有更有效的方法来制作多步动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆