在转换时悬停在图像上显示文本

时间:2016-09-21 17:22:54

标签: css css-transitions

我正在尝试使用不透明度创建淡入淡出过渡(请参阅我的JSFiddle),当您将鼠标悬停在圆圈上时会显示。

如何实现myContent元素的漂亮淡入?

<div class="circle">
  <div class="myContent">
    <div class="name">Sasasaas</div>
    <div class="position">asfasfasfas</div>
  </div>
</div>

这是我正在使用的CSS

.circle {
  width: 300px;
  height: 300px;
  border: 1px black solid;
  border-radius: 200px;
  background-image: url("http://vignette3.wikia.nocookie.net/yogscast/images/c/c7/Pixel_face_eyebrow_raised_600x600.png/revision/latest/scale-to-width-down/480?cb=20130529234139");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: contain;
  margin: auto;
  -webkit-filter: grayscale(50%);
  -webkit-transition: all 0.5s;
}

.circle:hover {
  -webkit-filter: none;
}

.myContent {
  opacity: 0;
  width: 170px;
  height: 170px;
  text-align: center;
  padding: 15px;
  /**position: relative;
    visibility: hidden;
    -webkit-transition: visibility 0.5s;**/
}

.circle:hover .myContent {
  opacity: 1;
}

1 个答案:

答案 0 :(得分:2)

尝试以下代码,将转换添加到.mycontent并设置width as 100%,然后使用padding-top:50%text-align:center添加padding-top将文本垂直和水平对齐到图像中心。

&#13;
&#13;
 .circle {
   width: 300px;
   height: 300px;
   border: 1px black solid;
   border-radius: 200px;
   background-image: url("http://vignette3.wikia.nocookie.net/yogscast/images/c/c7/Pixel_face_eyebrow_raised_600x600.png/revision/latest/scale-to-width-down/480?cb=20130529234139");
   background-position: center center;
   background-repeat: no-repeat;
   background-size: contain;
   margin: auto;
   -webkit-filter: grayscale(50%);
   -webkit-transition: all 0.5s;
 }
 
 .circle:hover {
   -webkit-filter: none;
 }
 
 .myContent {
   opacity: 0;
   width: 100%;
   height: 170px;
   text-align: center;
   padding-top: 50%;
   transition: 1s ease;
 }
 
 .circle:hover > .myContent {
   opacity: 1;
 }
&#13;
<div class="circle">
  <div class="myContent">
    <div class="name">Sasasaas</div>
    <div class="position">asfasfasfas</div>
  </div>
</div>
&#13;
&#13;
&#13;