我的演示将在 circle 上悬停时淡入 square。从那里开始,当您将鼠标悬停在 正方形 上时,它将保持不透明。离开圆形或方形后,方形会淡出。
让它工作的诀窍是为 square 的 opacity、height 和 width 属性设置 2 个不同的转换,一个用于 hover ON 和一个用于 hover OFF,以及为过渡添加一个delay 属性。转换height 和width 的原因是,它会阻止您在未先将鼠标悬停在圆上的情况下将鼠标悬停在正方形上。
方块的默认设置如下:opacity: 0、height: 0 和 width: 0。
对于 hover ON 过渡,您希望 opacity 淡入超过 1 秒,但要能够看到,height 和 width 值需要为 40px在淡入过渡之前。为此,您需要在height 和width 转换上设置0 秒的延迟。这样,正方形会立即处于其最大尺寸,从而可以看到淡入过渡。
hover OFF 过渡将恢复为默认设置。您希望opacity 在 1 秒内缓出,同时将 height 和 width 的值保持在 40px。否则,height 和 width 将立即恢复为 0,您将无法看到淡出过渡。要做到这一点,您需要在 height 和 width 转换上设置 1 秒的延迟。这样做时,opacity 会减少 1 秒以上,并且由于 height 和 width 的 1 秒延迟,此时,height 和 width 将恢复为 0。
HTML
<div id="gravatar">
<div id="circle"></div>
<div id="square"></div>
</div>
CSS
#gravatar
{
float: left;
}
#circle
{
background-color: blue;
float: left;
height: 40px;
width: 40px;
border-radius: 20px;
}
#square
{
background-color: red;
float: left;
margin-left: 10px;
height: 0;
width: 0;
opacity: 0;
/* hover OFF */
-webkit-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
-o-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
}
#square:hover,
#circle:hover + #square
{
height: 40px;
width: 40px;
opacity: 1;
/* hover ON */
-webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}
编辑
OP 留下了一条评论,指出向 square 添加内容会阻止转换正常工作。我通过将overflow: hidden 添加到正方形来更正它。
我还在 CSS 中添加了其他样式来说明 OP 添加的锚点。
HTML
<div id="gravatar">
<div id="circle"></div>
<div id="square">
<a href="http://techyoucation.com/?page_id=156">Profile Details</a>
<a href="http://techyoucation.com/?page_id=59">Account Details</a>
</div>
</div>
CSS
#gravatar
{
float: left;
}
#circle
{
background-color: blue;
float: left;
height: 40px;
width: 40px;
border-radius: 20px;
}
#square
{
background-color: #2D3538;
float:left;
overflow: hidden;
margin-left: 10px;
height: 0;
width: 0;
opacity: 0;
/* hover OFF */
-webkit-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
-o-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
}
#square > a
{
display: block;
font: 15px Verdana;
color: #FFF;
text-decoration: none;
height: 15px;
line-height: 15px;
margin: 10px;
}
#square > a:last-child
{
margin-top: 0;
}
#square > a:hover
{
text-decoration: underline;
}
#square:hover,
#circle:hover + #square
{
height: 60px;
width: 135px;
opacity: 1;
/* hover ON */
-webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}