简体   繁体   English

悬停CSS动画

[英]CSS Animation on hover out

I'm trying to get an element to animate using CSS when hovering out of an area. 我试图将元素悬停在某个区域外时使用CSS进行动画处理。 (I'm not familiar with how to do this in javascript) (我不熟悉如何在javascript中执行此操作)

I've done the hover animation using the hover pseudo element and keyframe animations. 我已经使用悬停伪元素和关键帧动画完成了悬停动画。 I'm trying to animate opacity on :hover and hover out. 我正在尝试在:hover上设置不透明度的动画并悬停。

I've got the :hover animation ok, but I want the animate the hover out effect. 我已经可以:hover动画了,但是我想给悬浮动画设置动画。 Is there a way to do this with CSS3? 有没有办法用CSS3做到这一点? I've tried a few things, but no luck. 我尝试了几件事,但是没有运气。

HTML: HTML:

<a href="#" class="post-header">    
  <h2 class="post-title">Header 
    <em>- Sub Header -</em>
  </h2>         
</a>

CSS: CSS:

a.post-header {
background: rgba(17,17,17,0.35);
bottom: 0;
left: 0;
right: 0;
color: #000000;
padding-left: 100px;
padding-right: 100px;
}

/*Fading out*/
@-webkit-keyframes fadeout {
    from { opacity: 1; }
    to { opacity: 0; }
}

@-moz-keyframes fadeout {
    from { opacity: 1; }
    to { opacity: 0; }
}

@keyframes fadeout {
    from { opacity: 1; }
    to { opacity: 0; }
}

h2.post-title em{
    display: none;
    -webkit-animation: fadeout 1.2s ease-in-out;
    -moz-animation: fadeout 1.2s ease-in-out;
    animation: fadeout 1.2s ease-in-out;
}

/*Fading in*/
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to { opacity: 1; }
}

@-moz-keyframes fadein {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes fadein {
    from { opacity: 0; }
    to { opacity: 1; }
}

a.post-header:hover em {
    display: inline-block;
    -webkit-animation: fadein 1.2s ease-in-out;
    -moz-animation: fadein 1.2s ease-in-out;
    animation: fadein 1.2s ease-in-out;
}

See the fiddle below 见下面的小提琴

jsfiddle 的jsfiddle

Thanks! 谢谢!

Yes. 是。 Use CSS Transitions instead of animations. 使用CSS Transitions而不是动画。

Then you can set it as follows: 然后,您可以将其设置如下:

h2.post-title em{
    -webkit-transition: opacity 1s;
    transition: opacity 1s;
    opacity: 0;
}

a.post-header:hover em {
    opacity: 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM