Switch between css classes with only 1 code

Asked

Viewed 35 times

-4

My scenario is as follows, I have 4 classes of animations and I need that regardless of my choice the div do your role, but this happening that if I run one the others does not work as should.

toggle always keeps the option selected and blank, is there any way to check or marry the classes so that only they change classes? ex: flip in with flip out, lightspeedin with out , and so on.

on the switch would be like whenever I click the button it check the active class and put the reverse to activate?

if this

function transicao1() {
  var element = document.getElementById("preview");
  element.classList.toggle("lightSpeedIn");
}

function transicao2() {
  var element = document.getElementById("preview");
  element.classList.toggle("lightSpeedOut");
}

function transicaoflip1() {
  var element = document.getElementById("preview");
  element.classList.toggle("flipInY");
}

function transicaoflip2() {
  var element = document.getElementById("preview");
  element.classList.toggle("flipOutY");
}
.lightSpeedIn {
  -webkit-animation-name: lightSpeedIn;
  animation-name: lightSpeedIn;
  -webkit-animation-timing-function: ease-out;
  animation-timing-function: ease-out;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -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: 1s;
  animation-duration: 1s;
  -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;
  }
  } 
  
.flipInY {
  -webkit-backface-visibility: visible !important;
  backface-visibility: visible !important;
  -webkit-animation-name: flipInY;
  animation-name: flipInY;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  }
  @-webkit-keyframes flipInY {
  0% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  -webkit-transition-timing-function: ease-in;
  transition-timing-function: ease-in;
  opacity: 0;
  }
  40% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
  transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
  -webkit-transition-timing-function: ease-in;
  transition-timing-function: ease-in;
  }
  60% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
  transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
  opacity: 1;
  }
  80% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  }
  100% {
  -webkit-transform: perspective(400px);
  transform: perspective(400px);
  }
  }
  @keyframes flipInY {
  0% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  -webkit-transition-timing-function: ease-in;
  transition-timing-function: ease-in;
  opacity: 0;
  }
  40% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
  transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
  -webkit-transition-timing-function: ease-in;
  transition-timing-function: ease-in;
  }
  60% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
  transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
  opacity: 1;
  }
  80% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  }
  100% {
  -webkit-transform: perspective(400px);
  transform: perspective(400px);
  }
  } 
  
  .flipOutY {
  -webkit-backface-visibility: visible !important;
  backface-visibility: visible !important;
  -webkit-animation-name: flipOutY;
  animation-name: flipOutY;
  -webkit-animation-duration: .75s;
  animation-duration: .75s;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  }
  @-webkit-keyframes flipOutY {
  0% {
  -webkit-transform: perspective(400px);
  transform: perspective(400px);
  }
  30% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
  transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
  opacity: 1;
  }
  100% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  opacity: 0;
  }
  }
  @keyframes flipOutY {
  0% {
  -webkit-transform: perspective(400px);
  transform: perspective(400px);
  }
  
  30% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
  transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
  opacity: 1;
  }
  
  100% {
  -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
  opacity: 0;
  }
  }
<button onClick="transicao1()"> APLICAR EFEITO DE ENTRADA</button>

<button onClick="transicao2()"> APLICAR EFEITO DE SAIDA </button>


<div id="preview" style="background-color: red"> DIV A SER TRANSICIONADA</div>


<button onClick="transicaoflip1()"> OUTRO EFEITO DE ENTRADA</button>

<button onClick="transicaoflip2()"> OUTRO EFEITO DE SAIDA </button>

2 answers

0

To make sure you have removed the other effects before applying the next ones, I suggest you remove the effect classes.

Also, to simplify the function, simply create the classes that indicate effect and direction (input and output).

In the example below, the function transicao takes the name of the effect and the direction as parameters. The function simply removes the effect and direction classes and adds the ones that came from the call:

function transicao(direction, effect) {
  //recebendo dois parametros apenas:
  // direction: 'in' OU 'out' - strings que representam se é de entrada ou saida
  // efecct 'ligthspeed' OU 'flip' - o tipo de efeito
  var element = document.getElementById("preview");
  element.classList.remove("in", "out", "ligthspeed", "flip"); //limpando tudo o que afeta
  element.classList.add(direction, effect); //acrescentando as escolhidas
}
/* sugiro mudar para classes independentes: .ligthspeed e .in e assim por diante*/
.ligthspeed.in { /* aqui a classe de efeito ligthspeed e de entrada */
  -webkit-animation-name: lightSpeedIn;
  animation-name: lightSpeedIn;
  -webkit-animation-timing-function: ease-out;
  animation-timing-function: ease-out;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -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;
  }
}

.ligthspeed.out {
  -webkit-animation-name: lightSpeedOut;
  animation-name: lightSpeedOut;
  -webkit-animation-timing-function: ease-in;
  animation-timing-function: ease-in;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -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;
  }
}

.flip.in {
  -webkit-backface-visibility: visible !important;
  backface-visibility: visible !important;
  -webkit-animation-name: flipInY;
  animation-name: flipInY;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}

@-webkit-keyframes flipInY {
  0% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    -webkit-transition-timing-function: ease-in;
    transition-timing-function: ease-in;
    opacity: 0;
  }
  40% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
    transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
    -webkit-transition-timing-function: ease-in;
    transition-timing-function: ease-in;
  }
  60% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
    transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
    opacity: 1;
  }
  80% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
    transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  }
  100% {
    -webkit-transform: perspective(400px);
    transform: perspective(400px);
  }
}

@keyframes flipInY {
  0% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    -webkit-transition-timing-function: ease-in;
    transition-timing-function: ease-in;
    opacity: 0;
  }
  40% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
    transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
    -webkit-transition-timing-function: ease-in;
    transition-timing-function: ease-in;
  }
  60% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
    transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
    opacity: 1;
  }
  80% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
    transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  }
  100% {
    -webkit-transform: perspective(400px);
    transform: perspective(400px);
  }
}

.flip.out {
  -webkit-backface-visibility: visible !important;
  backface-visibility: visible !important;
  -webkit-animation-name: flipOutY;
  animation-name: flipOutY;
  -webkit-animation-duration: .75s;
  animation-duration: .75s;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}

@-webkit-keyframes flipOutY {
  0% {
    -webkit-transform: perspective(400px);
    transform: perspective(400px);
  }
  30% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
    transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
    opacity: 1;
  }
  100% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    opacity: 0;
  }
}

@keyframes flipOutY {
  0% {
    -webkit-transform: perspective(400px);
    transform: perspective(400px);
  }
  30% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
    transform: perspective(400px) rotate3d(0, 1, 0, -15deg);
    opacity: 1;
  }
  100% {
    -webkit-transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    opacity: 0;
  }
}
<button onClick="transicao('in','ligthspeed')"> APLICAR EFEITO DE ENTRADA</button>

<button onClick="transicao('out','ligthspeed')"> APLICAR EFEITO DE SAIDA </button>


<div id="preview" style="background-color: red"> DIV A SER TRANSICIONADA</div>


<button onClick="transicao('in', 'flip')"> OUTRO EFEITO DE ENTRADA</button>

<button onClick="transicao('out', 'flip')"> OUTRO EFEITO DE SAIDA </button>

0

studying alternatives I ended up finding a more direct way just using the comparative if and Else

        function transicao_lightspeed(item){
           if(item.className == "lightSpeedIn") {
              item.className="lightSpeedOut";
           } else {
              item.className="lightSpeedIn";
           }
        }
.lightSpeedIn {
  -webkit-animation-name: lightSpeedIn;
  animation-name: lightSpeedIn;
  -webkit-animation-timing-function: ease-out;
  animation-timing-function: ease-out;
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -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: 1s;
  animation-duration: 1s;
  -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;
  }
  } 
<input type="button" value="Velocidade" class="" onclick="transicao_lightspeed(preview)" />

<br><br>

<div id="preview" style="margin:auto;width:50%;height:40px;font-size:32px;text-align:center;background-color:black;color:#fff;">Seu 
        Texto</div>

  • 1

    In case it also works, but has the disadvantage of you need to fix the function whenever you want to create new effects.

Browser other questions tagged

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