文字从左到右显示

Add*_*ddy 1 css css3 css-transitions

我想在图像悬停时出现图像的标题(不一定是真实的标题).文本应该从左到右显示,因此当图像悬停时,它的容器应该在X轴上生长.我已经能够使文本显示在悬停状态,但过渡效果是我被卡住的地方.
这是我尝试过的:

CSS:

.morbid {
  display: none;
  margin-left:10px;
}

a:hover + .morbid {
  position:relative;
  display: block;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<a>
  <img src='.../goals/sia.png'> 
</a>
<div class="morbid">
This text will appear as you hover your mouse over the image.
</div>
Run Code Online (Sandbox Code Playgroud)

实际上整个div必须增长,文本保持静态,我希望隐藏文本,除非图像悬停.

Har*_*rry 8

您无法转换display财产的价值变化.规范中提供可以设置动画的完整属性列表.

对于要增长和显示的内容,您需要转换max-width(或者width,如果您可以设置固定宽度到元素),就像在下面的代码段中完成的那样.

.morbid {
  width: auto;
  max-width: 0%;
  white-space: nowrap;
  overflow: hidden;
  transition: max-width 1s linear;
}
a:hover + .morbid {
  max-width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<a href="#">
  <img src='http://lorempixel.com/400/200'>
</a>
<div class="morbid">
  This text will appear as you hover your mouse over the image.
</div>
Run Code Online (Sandbox Code Playgroud)


或者,如果您希望文本从中心增长,那么您可以使用绝对定位,transform如下面的代码段所示.

.container {
  position: relative;
  width: 400px;
}
.morbid {
  position: absolute;
  left: 50%;
  max-width: 0%;
  white-space: nowrap;
  overflow: hidden;
  transform: translateX(-50%);
  transition: max-width 1s linear;
}
a:hover + .morbid {
  max-width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <a href="#">
    <img src='http://lorempixel.com/400/200'>
  </a>
  <div class="morbid">
    This text will appear as you hover your mouse over the image.
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)