CSS3 - 过渡效果幻灯片背景位置

CSS3 - Transition effect slide background position

我正在使用鼠标悬停时滑动 down/up 的过渡效果。这当然有道理,但我希望它像边框底部一样慢慢淡化in/out。

这是我的代码:

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>

编辑:根据您的评论

这是一个片段

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>

我认为当背景图像是 sprite 的一部分时,使用单个元素不可能实现 fade-in/out 效果(您试图创建的效果)。这是因为精灵总是需要滑动。为了使幻灯片移动不可见并发生淡入淡出,我们需要不止一个状态更改 - (1) fade-out 当前图像 (opacity: 0),(2) 将 background-position 更改为正确位置 (3) fade-in 图像 (opacity: 1)。由于它需要的不仅仅是不透明度的改变,所以它不能通过一个元素上的过渡来实现。 (注意:虽然可以用动画来完成)。

当我们使用 2 个元素(其中一个是 pseudo-element)时,我们可以实现您正在寻找的效果。我不太确定您要寻找的效果类型是什么(因为边框的变化不完全是 fade-in/out,它只是颜色变化)所以我给出了两个具有两种不同效果的示例。您可以选择这两种效果中适合您需要的一种

类似border bottom的效果:(就是图标好像变色了)

这是通过在黄色图标顶部添加一个灰色图标(使用 pseudo-element)然后在悬停时将其 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>


实际fade-in/out效果:(灰色图标淡出,悬停时黄色fades-in和vice-versa)

这是通过在黄色图标顶部添加灰色图标(使用 pseudo-element)再次完成的,悬停时,灰色图标的 opacity 首先更改为 0,然后延迟一段时间后,黄色图标的 opacity 变为 1(这是使用 transition-delay 等于 transition-duration 实现的)。

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>