How to make CSS Animation reverse on hover-out?

2019-07-18 04:40发布

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

2条回答
叼着烟拽天下
2楼-- · 2019-07-18 05:33

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

查看更多
forever°为你锁心
3楼-- · 2019-07-18 05:34

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;
}
查看更多
登录 后发表回答