CSS过渡,将宽度/高度折叠到div的中心

时间:2014-11-12 17:09:52

标签: html css3

以下是我的努力。

http://liveweave.com/i1qkNw

下面也是我的代码



.container {
  display: inline-block;
  position: relative;
  border: 1px solid green;
}
.middle {
  position: relative;
  width: 300px;
  height: 250px;
  background: black;
}
.door {
  position: absolute;
  width: 300px;
  height: 250px;
  background: red;
  top: 0;
  left: 0;
  opacity: 0.5;
  transition: .5s;
  transform-origin: center center;
}
.container:hover .door {
  transition: .5s;
  opacity: 0;
  width: 0;
  height: 0;
}

<div class="container">
  <div class="middle"></div>
  <div class="door"></div>

</div>
&#13;
&#13;
&#13;

我想要做的是,当用户将鼠标悬停在容器上时,我希望门div的宽度/高度为零。正如你所看到的,我实现了这个效果,但它消失在左上角。有什么办法可以让它消失在它的中心吗?像宽度和高度一样减小到中心并消失。

请指导我如何实现这种效果。

1 个答案:

答案 0 :(得分:1)

您必须将上/下/左/右值设置为50%

.container {
  display: inline-block;
  position: relative;
  border: 1px solid green;
}
.middle {
  position: relative;
  width: 300px;
  height: 250px;
  background: black;
}
.door {
  position: absolute;
  width: 300px;
  height: 250px;
  background: red;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0.5;
  transition: .5s;
  transform-origin: center center;
}
.container:hover .door {
  transition: .5s;
  opacity: 0;
  top: 50%;
  bottom: 50%;
  left: 50%;
  right: 50%;
  width: 0;
  height: 0;
}
<div class="container">
  <div class="middle"></div>
  <div class="door"></div>
</div>