0

I have 2 div tags that I turned into blocks. I made some code that when run, makes the 2 blocks swoop out to the side. My problem is, they then swoop back into their original position. I want the 2 blocks you see in the animation below, to swoop out to the sides, but then not return to the center.

:)

.dark1 {
  background-color: black;
  height: 100vh;
  position: absolute;
  left: 0px;
  top: 0px;
  width: 50%;
  animation: example1 5s;
}

.dark2 {
  background-color: red;
  height: 100vh;
  position: absolute;
  top: 0px;
  right: 0px;
  width: 50%;
  animation: example 5s;
}

@keyframes example {
  50% {
    right: -500px;
    top: 0px;
  }
}

@keyframes example1 {
  50% {
    left: -500px;
    top: 0px;
  }
}
<!DOCTYPE html>
<html>

<body>
  <div class="dark1"></div>
  <div class="dark2"></div>

</body>

</html>
JSman225
  • 96
  • 1
  • 16

3 Answers3

1

you can use animation-fill-mode: forwards

.dark1 {
  background-color: black;
  width: 50px;
  height: 100vh;
  position: absolute;
  left: 0px;
  top: 0px;
  width: 50%;
  animation: example1 5s;
  animation-fill-mode: forwards
}

.dark2 {
  background-color: red;
  width: 50px;
  height: 100vh;
  position: absolute;
  top: 0px;
  right: 0px;
  width: 50%;
  animation: example 5s;
  animation-fill-mode: forwards
}

@keyframes example {
 100% {
    right: -500px;
    top: 0px;
  }
}

@keyframes example1 {
  100% {
    left: -500px;
    top: 0px;
  }
}
<body>
  <div class="dark1"></div>
  <div class="dark2"></div>

</body>
Charles Lavalard
  • 2,061
  • 6
  • 26
1

The animation reverses because you specified 50% in your keyframe definition, and the default animation-fill-mode is none, so the animation will reset at the end.

Instead, make the keyframes defined at 100% (or to) and apply an animation-fill-mode of forwards or both:

.dark1 {
  background-color: black;
  width: 50px;
  height: 100vh;
  position: absolute;
  left: 0px;
  top: 0px;
  width: 50%;
  animation: example1 5s;
  animation-fill-mode: both;
}

.dark2 {
  background-color: red;
  width: 50px;
  height: 100vh;
  position: absolute;
  top: 0px;
  right: 0px;
  width: 50%;
  animation: example 5s;
  animation-fill-mode: both;
}

@keyframes example {
 100% {
    right: -500px;
  }
}

@keyframes example1 {
  100% {
    left: -500px;
  }
}
<!DOCTYPE html>
<html>

<body>
  <div class="dark1"></div>
  <div class="dark2"></div>

</body>

</html>
FZs
  • 16,581
  • 13
  • 41
  • 50
0

Also you can do this with single element and less code.

.box {
  background: linear-gradient(black 0 0) 0 0 / 50% 100% no-repeat, 
              linear-gradient(red 0 0) 100% 0 / 50% 100% no-repeat;
  height: 100vh;
  animation: example 5s forwards;
}

@keyframes example {
  to {
    background-size: 25% 100%;
  }
}
<div class="box"></div>
doğukan
  • 23,073
  • 13
  • 57
  • 69