Inconsistent Transitions
Published 13 years, 8 months pastHere’s an interesting little test case for transitions. Obviously you’ll need to visit it in a browser that supports CSS transitions, and additionally also CSS 2D transforms. (I’m not aware of a browser that supports the latter without supporting the former, but your rendering may vary.)
In Webkit and Gecko, hovering the first div
causes the span
to animate a 270 degree rotation over one second, but when you unhover the div
the span
immediately snaps back to its starting position. In Opera 11, the span
is instantly transformed when you hover and instantly restored to its starting position when you unhover.
In all three (Webkit, Gecko, and Opera), hovering the second div
triggers a one-second 270-degree rotation of the span
. Unhovering causes the rotation animation to be reversed; that is, a one-second minus-270-degree rotation — or, if you mouseout from the div
before the animation finishes, an rotation from that angle back to the starting position. Either way, it’s completely consistent across browsers.
The difference is that in the first test case, both the transform and the transition are declared on hover. Like this (edited for clarity):
div:hover span {
transition: 1s transform;
transform: rotate(270deg);
}
In the second test case, the transform and the transition are split up like so:
div span {
transition: 1s transform;
}
div:hover span {
transform: rotate(270deg);
}
It’s an interesting set of results. Only the second case is consistently animated across the tested browsers, but the first case only animates one direction in Webkit and Gecko. I’m not sure which, if any, of these results is more correct than the other. It could well be that they’re all correct, even if not consistent; or that they’re all wrong, just in different ways.
At any rate, the takeaway here is that you probably don’t want to apply your transition properties to the hover state of the thing you’re transitioning, but to the unhovered state instead. I say “probably” because maybe you like that it transitions on mouseover and instantly resets on mouseout. I don’t know that I’d rely on that behavior, though. It feels like the kind of thing that programmer action, or even spec changes, will take away.