CSS3 Animate Out of :hover

2020-05-25 07:22发布

问题:

I'm setting up a simple animation that brings up a little additional information when you hover over an image. The jQuery backup part is written and works fine, though the issue I'm having trouble with is getting the CSS3 animation to go in reverse when the user hovers out.

So when you hover in it works great, but as soon as you hover out the elements just disappear. What I'd like to know is if there's a way to get them to animate backwards when the mouse hovers elsewhere. Thanks a lot!

#recent-work div { position: relative; width: 300px; height: 168px; overflow: hidden; }
                
                #recent-work div:hover .recent-project-type {  
                    animation-name: showType;
                    animation-duration: .5s;
                    animation-timing-function: ease;
                    animation-delay: 0;
                    animation-iteration-count: 1;
                    animation-direction: normal;
                    animation-play-state: running;
                    -moz-animation-name: showType;
                    -moz-animation-duration: .5s;
                    -moz-animation-timing-function: ease;
                    -moz-animation-delay: 0;
                    -moz-animation-iteration-count: 1;
                    -moz-animation-direction: normal;
                    -moz-animation-play-state: running;
                    -webkit-animation-name: showType;
                    -webkit-animation-duration: .5s;
                    -webkit-animation-timing-function: ease;
                    -webkit-animation-delay: 0;
                    -webkit-animation-iteration-count: 1;
                    -webkit-animation-direction: normal;
                    -webkit-animation-play-state: running;
                    top: 0;
                }
                
                #recent-work div:hover .recent-project-title {  
                    animation-name: showTitle;
                    animation-duration: .5s;
                    animation-timing-function: ease;
                    animation-delay: 0;
                    animation-iteration-count: 1;
                    animation-direction: normal;
                    animation-play-state: running;
                    -moz-animation-name: showTitle;
                    -moz-animation-duration: .5s;
                    -moz-animation-timing-function: ease;
                    -moz-animation-delay: 0;
                    -moz-animation-iteration-count: 1;
                    -moz-animation-direction: normal;
                    -moz-animation-play-state: running;
                    -webkit-animation-name: showTitle;
                    -webkit-animation-duration: .5s;
                    -webkit-animation-timing-function: ease;
                    -webkit-animation-delay: 0;
                    -webkit-animation-iteration-count: 1;
                    -webkit-animation-direction: normal;
                    -webkit-animation-play-state: running;
                    bottom: 0;
                }
            
            .recent-project-title { position: absolute; left: 0; right: 0; bottom: -34px; padding: 8px 10px; background: rgba(0,0,0,.75); text-decoration: none; border: 0; font-size: 20px; font-weight: 400; color: #fff; }
                .recent-project-title:hover { color: #ff9900; text-decoration: none; }
                
            .recent-project-type { position: absolute; left: 0; top: -26px; padding: 4px 8px; font-size: 12px; font-weight: 600; background: #ff9900; text-transform: uppercase; color: #111; }
                .recent-project-type:hover { color: #fff; text-decoration: none; }

@keyframes showType {
    from { top: -26px; }
    to { top: 0; }
}

@-moz-keyframes showType {
    from { top: -26px; }
    to { top: 0; }
}

@-webkit-keyframes showType {
    from { top: -26px; }
    to { top: 0; }
}

@keyframes showTitle {
    from { bottom: -34px; }
    to { bottom: 0; }
}

@-moz-keyframes showTitle {
    from { bottom: -34px; }
    to { bottom: 0; }
}

@-webkit-keyframes showTitle {
    from { bottom: -34px; }
    to { bottom: 0; }
}
<div class="row" id="recent-work">
            <div class="span-one-third">
                <a href="#" class="recent-project-image"><img src="http://dl.dropbox.com/u/1762184/recent-work01.png" width="300" height="168"></a>
                <a href="#" class="recent-project-title">Philly</a>
                <a href="#" class="recent-project-type">Video</a>
            </div>
</div>

回答1:

You don't need keyframes for something as simple as this.

I made you a demo (with only -webkit vender prefixes to keep things simple).



回答2:

This can also be done with CSS transitions, it's less powerful but simpler. The idea is to have a div containing the top and bottom links but it is bigger than the wrapper div so that parts are hidden. When you hover over it, it reduces height so the links are visible. To make it animate back and forth you add the "transition : height 1s" to the css of the div. If I'll have the time later, I'll try to write it.



回答3:

I believe if you add the animation to the non-hover state, you can get them to transition back. See my super-simple example here.