i have a problem, when i hover on .wrapper__shopcart element its child element .wrapper__shopcart-modal will show up with animation:
@keyframes Growth{
from{
transform: scale(0);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
and when the hover is over i want it to disappear with the animation:
@keyframes Shrink{
from {
transform: scale(1);
opacity: 1;
}
to {
transform: scale(0);
opacity: 0;
}
}
but I don’t know how.
I wish an efficient css method for this problem
2
Answers
If you want this to happen on hover you need to use a
transition
, not a keyframe animation. See the below example.Also keep in mind that when it shrinks down to
scale(0)
that you really cannot "hover" over nothing any more so that animations might look odd if you move your mouse around.Here’s a way around that jerky animation though. Each element has a wrapping element. We hover over that one and only scale down the inner one.
@keyframes
are explicit set of instructions which will be executed once the page loads, and theanimation
properties dictates specific timing and frequency. So you’ll need a small amount of JavaScript to prevent the default state animation from running on page-load (the shrinking part) (see Example A).transitions
just dictates timing so it doesn’t start executing any animation on page-load. It’s triggered by an actual change of the style(s) it’s bound to so JavaScript isn’t needed (see Example B).Example A
Example B