悬停在文本上的CSS消失而不是像图像一样滑出

时间:2018-01-07 18:44:02

标签: css css3

有人可以解释为什么图像会很好地滑回来,当您将鼠标悬停时,文字会立即消失吗?

.wrapper {
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px; 
    border: 1px solid black;
}

#image {
    position: absolute;
    left: 0;
    width: 100px;
    height: 100px;
    background: blue;
    transition: 1s;
}

.wrapper:hover #image {
    transition: 1s;
    left: -100px;
}

.wrapper:hover .text {
    transition: 1s;
    left: 50%;
}

.text {
  white-space: nowrap; 
  //color: black;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 150%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}
<div class="wrapper">
    <img id="image" src="http://lorempixel.com/output/cats-q-c-100-100-4.jpg" />
    <div class="text">text</div>
</div>

所以我想要的是,文字在悬停时也很好地滑出,而不仅仅是消失。

1 个答案:

答案 0 :(得分:2)

您还需要将transition属性添加到.text

&#13;
&#13;
.wrapper {
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px; 
    border: 1px solid black;
}

#image {
    position: absolute;
    left: 0;
    width: 100px;
    height: 100px;
    background: blue;
    transition: 1s;
}

.wrapper:hover #image {
    transition: 1s;
    left: -100px;
}

.wrapper:hover .text {
    transition: 1s;
    left: 50%;
}

.text {
  white-space: nowrap; 
  //color: black;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 150%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transition: 1s;
}
&#13;
<div class="wrapper">
  <img id="image" src="http://lorempixel.com/output/cats-q-c-100-100-4.jpg" alt="">
  <div class="text">text</div>
</div>
&#13;
&#13;
&#13;