Is there a more efficient way to do multi-step animations?

纵然是瞬间 提交于 2019-12-08 07:19:32

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/50996921/is-there-a-more-efficient-way-to-do-multi-step-animations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!