2

I am trying to make an animation play reverse when hovering an element. It does not work, I don't understand why and how to get it to work. The "animation" just jumps back to keyframe 0% without any animation.

The snippet works only as intended in Chrome and Safari.

http://codepen.io/anon/pen/YpBaZq

div {
  -webkit-animation-fill-mode: forwards;
  animation-duration: 2s;
  animation-name: a;
  background-color: #a00;
  display: inline-block;
  padding: 50px;
}

div:hover {
  -webkit-animation-fill-mode: backwards;
  animation-duration: 2s;
  animation-name: a;
  animation-direction: reverse;
}

@keyframes a {
  0% {
    padding: 50px;
  }
  100% {
    padding: 100px 200px;
  }
}
<div></div>

This is just an extremely simplified example, so please don't suggest non-animation methods (except maybe an animation combined with a transition on :hover which I tried and couldn't get to work either).

This is much closer to the real-world code: https://codepen.io/connexo/pen/eBxMqO

I am also not interested in Javascript-based solutions, only CSS.

2
  • 1
    Shrinking on hover is problematic because the element may become not hovered, and thus create a loop or behave in some weird ways. Don't do this.
    – Oriol
    Commented Dec 19, 2016 at 15:06
  • As I said, it's just a simplified code example. This is much closer to my real use-case: codepen.io/connexo/pen/eBxMqO My intention was to keep the code as simple as possible for the purpose of asking.
    – connexo
    Commented Dec 19, 2016 at 15:25

1 Answer 1

4

Instead of using reverse I just created another animation which is the reverse of your current one.

I did it likes this: http://codepen.io/anon/pen/zoeWLO

div {
  -webkit-animation-fill-mode: forwards;
  animation-duration: 2s;
  animation-name: a;
  background-color: #a00;
  display: inline-block;
  padding: 50px;
}

div:hover {
  -webkit-animation-fill-mode: forwards;
  animation-duration: 2s;
  animation-name: ra;
}

@keyframes a {
  0% {
    padding: 50px;
  }
  100% {
    padding: 100px 200px;
  }
}

@keyframes ra{
    0% {
       padding: 100px 200px;
  }
  100% {
  padding: 50px;
 }
}
<div></div>

Correction: reverse is supported in Safari Desktop: https://developer.mozilla.org/en-US/docs/Web/CSS/animation-direction


This is my guess on the jumping behaviour using 'reverse' - animation for keyframe a is not played again on hovering.

The animation of keyframe a is at 100% after 2 seconds. Now you move your mouse in. With the effect of reverse, a at 100% is now 0%, which makes it become smaller. But since keyframe a is already done playing, the div would just jump to 0% instead of transitioning to 100%.

8
  • 1
    Still has the jumps.
    – roberrrt-s
    Commented Dec 19, 2016 at 15:03
  • @Roberrrt Oh, that's because I am experimenting with that code pen. Will experiment on another instance. Try now, it should work.
    – Alic
    Commented Dec 19, 2016 at 15:05
  • Stack Snippit, try hovering in and out fast, it doesn't respect the current value. I'm not sure it's possible in CSS though.
    – roberrrt-s
    Commented Dec 19, 2016 at 15:06
  • @Roberrrt Well, that's because it only works on hover. If you move out during the animation, you force the hover animation to stop and the normal one to start, which explains the jumping.
    – Alic
    Commented Dec 19, 2016 at 15:09
  • Yeah, just thinking out loud here, would it be possible to freeze the current animation location and have it return to the original value on hover out?
    – roberrrt-s
    Commented Dec 19, 2016 at 15:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.