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

时间:2016-01-10 01:00:27

标签: css css3 css-transitions

我有一个过渡效果,可以在悬停时向下滑动。当然这是有道理的,但我希望它像边界底部一样缓慢淡入/淡出。

这是我的代码:

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>

2 个答案:

答案 0 :(得分:1)

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

当我们使用2个元素(其中一个是伪元素)时,我们可以达到您正在寻找的效果。我不太确定你正在寻找什么类型的效果(因为边框的变化不是淡入/淡出,只是颜色变化)所以我给出两个样本两种不同的效果。您可以选择这两种效果中的任何一种适合您的需求

效果类似于边框底部的效果:(也就是说,图标看起来正在改变颜色)

这是通过在黄色图标的顶部添加灰色图标(使用伪元素),然后在悬停时将其opacity更改为0来完成的。不透明度变化意味着当悬停开启时,底部的黄色图标会进入视图。

&#13;
&#13;
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;
}
&#13;
<div class="bar-logout">
  <span>
<span class="logout"><div class="logout-icon"></div>LOGOUT</span>
  </span>
</div>
&#13;
&#13;
&#13;

实际淡入/淡出效果(即灰色图标淡出,悬停时黄色淡入,反之亦然)

再次通过在黄色图标顶部添加灰色图标(使用伪元素)来完成此操作,并且在悬停时,灰色图标的opacity首先更改为0然后延迟后黄色图标的opacity更改为1(这是使用等于transition-delay的{​​{1}}来实现的。

&#13;
&#13;
transition-duration
&#13;
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;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

编辑:根据您的评论

这是一个代码段

&#13;
&#13;
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 }
&#13;
<div class="bar-logout">
<span>
<span class="logout"><div class="logout-icon"></div>LOGOUT</span>
</span>
</div>
&#13;
&#13;
&#13;