-4

Meu cenário é o seguinte, tenho 4 classes de animações e necessito que independente da minha escolha a div faça o seu papel, porem esta acontecendo de que se eu executar uma as outras não funciona como deve.

a funcao toggle sempre mantem a opcao selecionada e a em branco, ha alguma maneira de verificar ou casar as classes para que somente elas troquem de classe? ex: flip in com flip out, lightspeedin com out , e assim vai.

no switch teria como sempre que eu clicar no botao ele verificar a classe ativa e colocar a contraria para ativar?

se este

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 Respostas 2

0

Para ter certeza que você removeu os outros efeitos antes de aplicar os próximos, sugiro que você remova as classes de efeito.

Também, para simplificar a função, basta criar as classes que indicam efeito e direção (entrada e saída).

No exemplo abaixo, a função transicao recebe como parametros o nome do efeito e o direção. A função simplesmente remove as classes de efeito e direção e acrescenta as que vieram da chamada:

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

estudando alternativas acabei encontrando uma maneira mais direta apenas utilizando o comparativo if e 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
  • 1
    No caso também funciona, mas tem a desvantagem de você precisar corrigir a função sempre que quiser criar novos efeitos. Commented 2/09/2021 às 17:21

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .