CSS3顺序动画实现父子元素动画效果

3
如何使子元素的动画在父元素完成其动画后发生,即父div淡入,然后1秒后其子元素淡入?
我尝试了很多技巧,但什么都不起作用,子元素仍然与父元素同时淡入。
请参见此处:http://jsfiddle.net/oeLbh43o/ HTML:
<div class="parent">
    <h1>The Title Here</h1>
</div>

CSS:

.parent {
    width: 250px;
    height: 250px;
    background: #fff;
    border: 1px solid #000;
    -webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    -moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    padding: 10px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}

.parent h1 {
    -webkit-transition: all 0.9s ease-in-out;
    -moz-transition: all 0.9s ease-in-out;
    transition: all 0.9s ease-in-out;
    opacity: 0;
}

.parent:hover {
    width: 300px;
    height: 300px;
}
.parent:hover h1 {
    opacity: 1;
    -webkit-animation-delay: 5s;
    -moz-animation-delay: 5s;
    animation-delay: 5s;
}

非常感谢

1个回答

3
您正在使用CSS过渡效果,因此应该指定transition-delay而不是animation-delay

.parent {
    width: 250px;
    height: 250px;
    background: #fff;
    border: 1px solid #000;
    -webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    -moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    padding: 10px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
    -webkit-transition-delay: 2s;
    -moz-transition-delay: 2s;
    transition-delay: 2s;
}

.parent h1 {
    -webkit-transition: all 0.9s ease-in-out;
    -moz-transition: all 0.9s ease-in-out;
    transition: all 0.9s ease-in-out;
    opacity: 0;
    -webkit-transition-delay: 0;
    -moz-transition-delay: 0;
    transition-delay: 0;
}

.parent:hover {
    width: 300px;
    height: 300px;
    -webkit-transition-delay: 0;
    -moz-transition-delay: 0;
    transition-delay: 0;
}
.parent:hover h1 {
    opacity: 1;
    -webkit-transition-delay: 2s;
    -moz-transition-delay: 2s;
    transition-delay: 2s;
}
<div class="parent">
    <h1>The Title Here</h1>
</div>


谢谢 @MatthewG。那反过来呢,当 mouseleave 时,即在父元素变小之前,子元素应该先淡出吗? - Shaoz
在这种情况下,您还想为非悬停状态指定转换延迟。因此,请确保反转哪个没有延迟,哪个有一些延迟。请参阅我的答案,我已更新它以给您这个示例。 - MatthewG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接