How to make CSS Animation reverse on hover-out?

2019-07-18 05:15发布

问题:

I have an animation on my site which goes from width: 100px to 800px when hover on.

But when hover out, it just goes to the normal position with no animation.

How could I get it so the animation would go back on hover-out the same way it came on hover?

I only found solutions for transitions, but I need it for animation.

<div id="blackbox"/>
<div id="blacktxt">
Navigation
</div>

See Here

回答1:

Why not use transitions instead of animations? Working jsFiddle

#blackbox {
    background: black;
    width: 100px; 
    height: 80px; 
    color: white; 
    text-align: center; 
    font-family: Times; 
    font-size: 20px;
    position: fixed;
    left: 0px;
    bottom: 100px;
    opacity: 0.5;
    margin-bottom: 10px;
    transition: all 2s; /* Add this transition */
}


#blackbox:hover { /* Apply the new design on hover */
    opacity: 0.8;
    width: 800px;
}

#blacktxt {
    margin-top: 10px;
    height: 60px;
    transition: opacity 0.5s, width 5s;
    position: absolute;
    font-family: cursive;
    cursor: default;
}

#blacktxt:hover {
    opacity: 0;
}


回答2:

I wrote the jQuery Reversible plugin for this. Its main advantage over CSS transitions is that it works on IE9 and older browsers.