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

▼魔方 西西 提交于 2019-12-08 20:07:31

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