我的演示将在盘旋圆圈时淡入正方形。从那里,当您将鼠标悬停在正方形上时,它将保持不透明。离开圆形或方形后,方形将淡出。
让它工作的技巧是为squareopacity
的、height
和width
属性设置 2 种不同的转换,一种用于悬停 ON,一种用于悬停 OFF,以及为转换添加一个属性。过渡的原因是它会阻止您在没有先将鼠标悬停在圆圈上的情况下将鼠标悬停在正方形上。delay
height
width
以下是正方形的默认设置:opacity: 0
、height: 0
和width: 0
。
对于悬停 ON过渡,您希望opacity
在 1 秒内淡入,但为了能够看到这一点,height
和width
值需要在淡入过渡之前 40 像素。要做到这一点,您需要在height
和width
转换上设置 0 秒的延迟。这样,正方形立即处于其最大尺寸,从而可以看到淡入过渡。
悬停关闭过渡将恢复为默认设置。你想要发生的事情是opacity
在 1 秒内缓出,同时保持40pxheight
和width
40px 的值。否则,height
andwidth
会立即恢复为 0,您将无法看到淡出过渡。要做到这一点,您需要在height
和width
转换上设置 1 秒的延迟。在这样做的过程中,opacity
由于 1 秒的延迟height
和1 秒后缓和width
,此时,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 留下了一条评论,指出向正方形添加内容会阻止转换正常工作。我通过添加overflow: hidden
到square来纠正它。
我还在 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;
}