我有一个在悬停时向下/向上滑动的过渡效果。当然,这是有道理的,但我希望它像边界底部一样慢慢淡出。
这是我的代码:
body {
background-color: rgb(6, 7, 11);
}
.bar-logout {
float: left;
background: rgb(7, 8, 13);
width: auto;
height: 40px;
border-bottom: 1px solid rgb(112, 101, 58);
cursor: pointer;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
.bar-logout span {
float: right;
padding: 6px 10px 6px 10px;
}
.bar-logout span span.logout {
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-size: 11px;
font-weight: bold;
color: rgb(164, 157, 139);
padding-top: 9px;
text-shadow: 0px 2px 3px rgb(0, 0, 0);
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
.bar-logout span span.logout .logout-icon {
float: left;position:relative;bottom:2px;background-image:url('http://i.imgur.com/eCXybOC.png');background-repeat:no-repeat;background-position:0 -16px;margin:0 8px 0 0;width:16px;height:16px;-webkit-transition:all 0.5s ease-in-out;-moz-transition:all 0.5s ease-in-out;-ms-transition:all 0.5s ease-in-out;-o-transition:all 0.5s ease-in-out;transition:all 0.5s ease-in-out; }
.bar-logout: hover {
border-bottom: 1px solid rgb(200, 180, 85);
}
.bar-logout:hover > span span.logout .logout-icon {
background-position: 0 0;
}
<div class="bar-logout">
<span>
<span class="logout"><div class="logout-icon"></div>LOGOUT</span>
</span>
</div>
当背景图像是精灵的一部分时,我认为不可能使用单个元素来实现淡入/淡出效果(您正在尝试创建)。这是因为精灵总是需要滑动。为了使幻灯片移动不可见并发生淡入淡出,我们需要不止一个状态更改-(1)淡出当前图像(opacity: 0
),(2)将background-position
更改到正确的位置(3)在图像中淡入淡出(opacity: 1
)。由于它需要的不仅仅是不透明度的更改,因此无法通过一个元素上的过渡来实现。(注意:不过也可以用动画来完成)。
当我们使用2个元素(其中一个是伪元素)时,我们可以达到您想要的效果。我不太确定你想要的效果类型是什么(因为边界的变化并不完全是淡入/淡出,它只是一种颜色变化),所以我给出了两个具有两种不同效果的样本。您可以选择这两种效果中适合您需要的效果。
效果类似于边框底部的效果:(即图标看起来像是在变色)
这是通过在黄色图标的顶部添加一个灰色图标来完成的(使用伪元素),然后在悬停时将其opacity
更改为0。不透明度的变化意味着当鼠标悬停时,底部的黄色图标会出现在视野中
body {
background-color: rgb(6, 7, 11);
}
.bar-logout {
float: left;
background: rgb(7, 8, 13);
width: auto;
height: 40px;
border-bottom: 1px solid rgb(112, 101, 58);
cursor: pointer;
transition: all 0.5s ease-in-out;
}
.bar-logout span {
float: right;
padding: 6px 10px 6px 10px;
}
.bar-logout span span.logout {
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-size: 11px;
font-weight: bold;
color: rgb(164, 157, 139);
padding-top: 9px;
text-shadow: 0px 2px 3px rgb(0, 0, 0);
transition: all 0.5s ease-in-out;
}
.bar-logout span span.logout .logout-icon {
float: left;
position: relative;
bottom: 2px;
background-image: url('http://i.imgur.com/eCXybOC.png');
background-repeat: no-repeat;
background-position: 0 0px;
margin: 0 8px 0 0;
width: 16px;
height: 16px;
transition: all 0.5s ease-in-out;
}
.bar-logout span span.logout .logout-icon:after {
position: absolute;
content: '';
background-image: url('http://i.imgur.com/eCXybOC.png');
background-repeat: no-repeat;
background-position: 0 -16px;
width: 100%;
height: 100%;
transition: all 0.5s ease-in-out;
z-index: 1;
}
.bar-logout:hover {
border-bottom: 1px solid rgb(200, 180, 85);
}
.bar-logout:hover > span span.logout .logout-icon:after {
opacity: 0;
}
<div class="bar-logout">
<span>
<span class="logout"><div class="logout-icon"></div>LOGOUT</span>
</span>
</div>
实际淡入/淡出效果:(即灰色图标淡出,悬停时黄色淡入,反之亦然)
这再次通过在黄色图标的顶部添加灰色图标来完成(使用伪元素),并且在悬停时,灰色图标的opacity
首先变为0,然后在延迟之后,黄色图标的opacity
变为1(这是使用等于transition-duration
的transition-delay
来实现的)。
body {
background-color: rgb(6, 7, 11);
}
.bar-logout {
float: left;
background: rgb(7, 8, 13);
width: auto;
height: 40px;
border-bottom: 1px solid rgb(112, 101, 58);
cursor: pointer;
transition: all 0.5s ease-in-out;
}
.bar-logout span {
float: right;
padding: 6px 10px 6px 10px;
}
.bar-logout span span.logout {
position: relative;
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-size: 11px;
font-weight: bold;
color: rgb(164, 157, 139);
padding-top: 9px;
text-shadow: 0px 2px 3px rgb(0, 0, 0);
transition: all 0.5s ease-in-out;
}
.bar-logout span span.logout .logout-icon {
float: left;
position: relative;
bottom: 2px;
background-image: url('http://i.imgur.com/eCXybOC.png');
background-repeat: no-repeat;
background-position: 0 0px;
margin: 0 8px 0 0;
width: 16px;
height: 16px;
opacity: 0;
transition: all 0.5s ease-in-out;
}
.bar-logout span span.logout:before {
float: left;
position: absolute;
content: '';
left: 10px;
bottom: 8px;
background-image: url('http://i.imgur.com/eCXybOC.png');
background-repeat: no-repeat;
background-position: 0 -16px;
margin: 0 8px 0 0;
width: 16px;
height: 16px;
transition: all 0.5s ease-in-out 0.5s;
z-index: 1;
}
.bar-logout:hover {
border-bottom: 1px solid rgb(200, 180, 85);
}
.bar-logout:hover > span span.logout:before {
opacity: 0;
transition: all 0.5s ease-in-out;
}
.bar-logout:hover > span span.logout .logout-icon{
opacity: 1;
transition: all 0.5s ease-in-out 0.5s;
}
<div class="bar-logout">
<span>
<span class="logout"><div class="logout-icon"></div>LOGOUT</span>
</span>
</div>
这是一个片段
body { background-color:rgb(6,7,11); }
.bar-logout { float:left;background:rgb(7,8,13);width:auto;height:40px;border-bottom:1px solid rgb(112,101,58);cursor:pointer;-webkit-transition:all 0.5s ease-in-out;-moz-transition:all 0.5s ease-in-out;-ms-transition:all 0.5s ease-in-out;-o-transition:all 0.5s ease-in-out;transition:all 0.5s ease-in-out; }
.bar-logout span { float:right;padding:6px 10px 6px 10px; }
.bar-logout span span.logout { font-family:"Trebuchet MS",Helvetica,sans-serif;font-size:11px;font-weight:bold;color:rgb(164,157,139);padding-top:9px;text-shadow:0px 2px 3px rgb(0,0,0);-webkit-transition:all 0.5s ease-in-out;-moz-transition:all 0.5s ease-in-out;-ms-transition:all 0.5s ease-in-out;-o-transition:all 0.5s ease-in-out;transition:all 0.5s ease-in-out; }
.bar-logout span span.logout .logout-icon { float:left;position:relative;bottom:2px;background-image:url('http://i.imgur.com/eCXybOC.png');background-repeat:no-repeat;background-position:0 -16px;margin:0 8px 0 0;width:16px;height:16px;-webkit-transition:opacity 0.5s ease-in-out;-moz-transition:opacity 0.5s ease-in-out;-ms-transition:opacity 0.5s ease-in-out;-o-transition:all 0.5s ease-in-out;transition:all 0.5s ease-in-out;opacity:0; }
.bar-logout:hover { border-bottom:1px solid rgb(200,180,85); }
.bar-logout:hover > span span.logout .logout-icon { opacity:1 }
<div class="bar-logout">
<span>
<span class="logout"><div class="logout-icon"></div>LOGOUT</span>
</span>
</div>