¿Es posible revertir una animación css al eliminar una clase?


Esencialmente lo que estoy tratando de hacer es darle a un elemento una animación CSS cuando gana una clase, luego revertir esa animación cuando elimino la clase sin reproducir la animación cuando el DOM renderiza.

Violín aquí: http://jsfiddle.net/bmh5g/

Como puedes ver en el violín, cuando cierras el botón "Hover Me", #item se voltea hacia abajo. Cuando usted mouseoff el botón hover, #item simplemente desaparece. Quiero #item voltear hacia arriba (idealmente usando la misma animación pero a la inversa). Es esto posible?

HTML:

<div id='trigger'>Hover Me</div>
<div id='item'></div>

CSS:

#item
{
    position: relative;
    height: 100px;
    width: 100px;
    background: red;

    -webkit-transform: perspective(350px) rotateX(-90deg);
    transform: perspective(350px) rotateX(-90deg);
    -webkit-transform-origin: 50% 0%;
    transform-origin: 50% 0%;
}

#item.flipped
{
    animation: flipper 0.7s;
    animation-fill-mode: forwards;
    -webkit-animation: flipper 0.7s;
    -webkit-animation-fill-mode: forwards;
}

@keyframes flipper
{
    0% { transform: perspective(350px) rotateX(-90deg); }
    33% { transform: perspective(350px) rotateX(0deg); }
    66% { transform: perspective(350px) rotateX(10deg); }
    100% { transform: perspective(350px) rotateX(0deg); }
}

@-webkit-keyframes flipper
{
    0% { -webkit-transform: perspective(350px) rotateX(-90deg); }
    33% { -webkit-transform: perspective(350px) rotateX(0deg); }
    66% { -webkit-transform: perspective(350px) rotateX(10deg); }
    100% { -webkit-transform: perspective(350px) rotateX(0deg); }
}

Javascript:

$('#trigger').on({
    mouseenter: function(){
        $('#item').addClass('flipped');
    },
    mouseleave: function(){
        $('#item').removeClass('flipped');
    }
})
Author: Jake, 2013-08-02

5 answers

Tendría el #item comenzar oculto con la animación inversa por defecto. Luego agregue la clase para darle la animación y mostrar el #item. http://jsfiddle.net/bmh5g/12 /

JQuery

$('#trigger').on({
    mouseenter: function(){
        $('#item').show();
        $('#item').addClass('flipped');
    },
    mouseleave: function(){
        $('#item').removeClass('flipped');
    }
});

CSS

#item
{
    position: relative;
    height: 100px;
    width: 100px;
    background: red;
    display: none;
    -webkit-transform: perspective(350px) rotateX(-90deg);
    transform: perspective(350px) rotateX(-90deg);
    -webkit-transform-origin: 50% 0%;
    transform-origin: 50% 0%;
    animation: flipperUp 0.7s;
    animation-fill-mode: forwards;
    -webkit-animation: flipperUp 0.7s;
    -webkit-animation-fill-mode: forwards;
}
 16
Author: Blake Plumb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-02 19:09:38

Otro enfoque, en lugar de usar display: none, es suprimir la animación inversa con una clase al cargar la página, y luego eliminar esa clase con el mismo evento que aplica la animación normal (por ejemplo: flipper). Así ( http://jsfiddle.net/astrotim/d7omcbrz/1/):

CSS-además del fotograma clave flipperUp publicado por Blake arriba

#item.no-animation 
{
  animation: none;
}

JQuery

$('#trigger').on({
    mouseenter: function(){
        $('#item').removeClass('no-animation');
        $('#item').addClass('flipped');
    },
    mouseleave: function(){
        $('#item').removeClass('flipped');
    }
})
 5
Author: Astrotim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-01 23:47:38

Además de las respuestas aquí, por favor guarde en caché su $(selector)

Así que básicamente haces esto var elements = $(selector); para almacenar en caché.

¿Por qué?! Porque si utilizas el código en las respuestas de esta página tal cual le pedirás al DOM esa misma colección de elementos ($('#item')) cada vez. La lectura de DOM es una operación costosa.

Por ejemplo, la respuesta aceptada se vería algo así:

var item = $('#item');
$('#trigger').on({
    mouseenter: function(){
        item.show();
        item.addClass('flipped');
    },
    mouseleave: function(){
        item.removeClass('flipped');
    }
});

Ya que he escrito todo este texto, también podría responder a su pregunta usando CSS transiciones

Sé que pediste un ejemplo de animaciones CSS, pero para la animación que querías hacer (una tarjeta abierta), se puede lograr fácilmente usando transiciones CSS:

#item {
  width: 70px;
  height: 70px;
  background-color: black;
  line-height: 1;
  color: white;
}

#item+div {
  width: 70px;
  height: 100px;
  background-color: blue;
  transform: perspective(250px) rotateX(-90deg);
  transform-origin: 50% 0%;
  transition: transform .25s ease-in-out
}

#item:hover+div {
  transform: perspective(250px) rotateX(0);
}
<div id="item"></div>
<div></div>
 3
Author: Kitanga Nday,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-16 21:50:59

Se está animando hacia abajo usando css, por lo que para que se anime hacia arriba necesita crear una clase, por ejemplo .item-up que hace la transformación en el sentido opuesto, por lo que entonces eliminaría la clase anterior y agregaría la clase item-up y eso debería animarla.

Le escribiría un violín js para ello, pero no conozco la sintaxis lo suficientemente bien.

Básicamente cuando se necesita:

@keyframes flipper
@keyframes flipper-up  //This does the opposite of flipper

Y

$('#trigger').on({
    mouseenter: function(){
        $('#item').removeClass('flipped-up');
        $('#item').addClass('flipped');
    },
    mouseleave: function(){
        $('#item').removeClass('flipped');
        $('#item').addClass('flipped-up');
    }
})

Jsfiddle.net/bmh5g/3, cortesía de Jake

 0
Author: Shane,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-02 18:50:43

Solución CSS de MDN y casi soportada por todos los navegadores

.animación (animationName 10s ease-in-out infinite alternate both running;)

 0
Author: B Isaac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-25 18:16:51