Switch between classes in pure javascript

Asked

Viewed 18 times

-5

I’m having trouble making switches alternating classes.

<script>
function toggletransicao() {
   var element = document.getElementById("myDIV");
   if(element.classList.contains('lightSpeedIn') == true) {
   element.classList.toggle("lightSpeedOut");
    } else {
    element.classList.add("lightSpeedIn");
    }
  }
</script>
       .lightSpeedIn {
  -webkit-animation-name: lightSpeedIn;
  animation-name: lightSpeedIn;
  -webkit-animation-timing-function: ease-out;
  animation-timing-function: ease-out;
  -webkit-animation-duration: 3s;
  animation-duration: 3s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  }
  @-webkit-keyframes lightSpeedIn {
  0% {
  -webkit-transform: translate3d(100%, 0, 0) skewX(-30deg);
  transform: translate3d(100%, 0, 0) skewX(-30deg);
  opacity: 0;
  }
  60% {
  -webkit-transform: skewX(20deg);
  transform: skewX(20deg);
  opacity: 1;
  }
  80% {
  -webkit-transform: skewX(-5deg);
  transform: skewX(-5deg);
  opacity: 1;
  }
  100% {
  -webkit-transform: none;
  transform: none;
  opacity: 1;
  }
  }
  @keyframes lightSpeedIn {
  0% {
  -webkit-transform: translate3d(100%, 0, 0) skewX(-30deg);
  transform: translate3d(100%, 0, 0) skewX(-30deg);
  opacity: 0;
  }
  60% {
  -webkit-transform: skewX(20deg);
  transform: skewX(20deg);
  opacity: 1;
  }
  80% {
  -webkit-transform: skewX(-5deg);
  transform: skewX(-5deg);
  opacity: 1;
  }
  100% {
  -webkit-transform: none;
  transform: none;
  opacity: 1;
  }
  } 
  
  .lightSpeedOut {
  -webkit-animation-name: lightSpeedOut;
  animation-name: lightSpeedOut;
  -webkit-animation-timing-function: ease-in;
  animation-timing-function: ease-in;
  -webkit-animation-duration: 3s;
  animation-duration: 3s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  }
  @-webkit-keyframes lightSpeedOut {
  0% {
  opacity: 1;
  }
  100% {
  -webkit-transform: translate3d(100%, 0, 0) skewX(30deg);
  transform: translate3d(100%, 0, 0) skewX(30deg);
  opacity: 0;
  }
  }
  @keyframes lightSpeedOut {
  0% {
  opacity: 1;
  }
  100% {
  -webkit-transform: translate3d(100%, 0, 0) skewX(30deg);
  transform: translate3d(100%, 0, 0) skewX(30deg);
  opacity: 0;
  }
  } 
<button onclick="toggletransicao()">ENTRADA / SAIDA</button>

<div id="myDIV" class=""> 
  <div align="center">uma div qualquer</div>
</div>

  • The correct spelling is toggle (with 2 "G"s), in the code was written "toogle" with two Os, this is wrong.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.