how to revese a css-animation after trigger it?

Viewed 58

Good evening, I'm a beginner front-end developer who is trying to implement sketches of Pinterest for training. I've found this gif for the signup/login page but I have a major issue for implementing animation, I don't have any idea for reversing the animation when the user clicks on the signup button can anybody tell me how can I reverse an animation?

function animation() {
    var element = document.getElementById("containerForm")
    element.classList.add("slide")
}
.containerForm {
  width:50px;height:50px;background-color:red;
}
.slide {
    animation: slide 2s ease-in-out;
    position: relative;
    animation-fill-mode: forwards;
}

@keyframes slide {
    0% {
        transform: translateX(0);
    }
    100% {
        transform: translateX(100%);
        overflow: hidden;
        visibility: visible;
    }
}
<div class="containerForm" id="containerForm">
      <form></form>
</div>
<button onclick="animation()">click</button>

but as I've said I don't have an idea for the second step

Link of gif

4 Answers

you can add another css class and make use of the animation-direction property. ie

.slide-back{
  animation: slide 2s ease-in-out;
  position: relative;
  animation-direction: reverse;
}

function animation() {
    var element = document.getElementById("containerForm")
    element.classList.add("slide")
}
function reverse() {
    var element = document.getElementById("containerForm")
    element.classList.remove("slide")
}
.containerForm {
    width:50px;
    height:50px;
    background-color:red;
    transform: translateX(0);
    transition: transform 2s;
}
.slide {
    transform: translateX(100%);
}
<div class="containerForm" id="containerForm">
      <form></form>
</div>
<button onclick="animation()">click</button>
<button onclick="reverse()">click</button>

transition works too:

example:

*{margin:0;}
#tst {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  min-height: 100vh;
  background: #555;
}

label {
  margin: auto;
  appearance: button;
  cursor: pointer;
  min-width: 6em;
  text-align: center;
}

#a,
#b {/*hides radios */
  position: absolute;
  right: 100vw;
}

[for="a"],
#c {
  grid-row: 1;
  grid-column: 1;
}

#c {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr;
  background: white;
  border: solid;
  transition: 0.5s;
  font-size:2vw;
}

#a:checked~#c {
  transform: translatex(0)
}

#b:checked~#c {
  transform: translatex(100%)
}

h1 {
  margin: auto;
}

#c p[class] {
  margin: auto;
  grid-column: 1;
  grid-row: 2;
  transition: 0.25s;
  background: inherit;
  color: #164fb7;
  font-size: 3vw;
}

#c p.b {
  opacity: 0;
  color: tomato;
}

#b:checked~#c p.b {
  opacity: 1
}
/* there were no js nor animation nor absolute positionning ;) */
<div id="tst">
  <input id="a" type=radio name=toggle>
  <input id="b" type=radio name=toggle>
  <label for="a">Left</label>
  <label for="b">Right</label>
  <div id="c">
    <h1>here or there ?</h1>
    <p class="a">Standing on the left</p>
    <p class="b">Standing on the right</p>
  </div>
</div>

Instead of adding a class I suggest using the animation-play-state property and adding event listeners:

  • one click listener to the button
  • one animationiteration to the div
  • simply change the play state from paused to running Let me show you what I mean:

const element = document.getElementById("containerForm");
const btn = document.querySelector("button");

function animation() {
  element.style.animationPlayState = "running";
}

function stopAnimation() {
  element.style.animationPlayState = "paused";
}

btn.addEventListener("click", animation);
element.addEventListener("animationiteration", stopAnimation);
  .containerForm {
  width: 50px;
  height: 50px;
  background-color: red;
  animation: slide 2s ease-in-out infinite alternate paused;
}

@keyframes slide {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(100%);
  }
<div class="containerForm" id="containerForm">
  <form></form>
</div>
<button>click</button>

Related