html - CSS 过渡和自动定位

标签 html css

我有一个 div .box,它的绝对位置设置在父 div 的底部。它没有设置顶部位置,因为不同的 .box div 的高度不同。将鼠标悬停在父 div 上时,我想通过过渡效果将其 top 位置更改为 0。

在下面的代码中,如果我没有默认定义顶部位置,则转换将不起作用。我试过使用 top: auto 但它仍然不起作用。如何在不定义顶部位置的情况下使用过渡。

这是我的代码:

HTML:

<div class="wrap">
    <div class="box">
        Box
    </div>
</div>

CSS:

.wrap{
    position: relative;
    height: 200px;
    width: 200px;
    background: green;
}

.box{
    background: yellow;
    position: absolute;
    bottom: 0;
    left: 0;
    transition: all  0.9s ease 0s;
    top: 50%; /*== does not work without it ==*/
}

.wrap:hover .box{
    top: 0;
    bottom: 0;
}

fiddle : http://jsfiddle.net/5hs09apn/

最佳答案

与使用 top 略有不同的方法:http://jsfiddle.net/5hs09apn/2/

设置框的高度,悬停时将其设置为100%;让底部为零,所以你甚至不需要使用 top

.box{
    background: yellow;
    position: absolute;
    bottom: 0;
    left: 0;
   height:20px;
    transition: all  1s ease 0s;
}

.wrap:hover .box{
   height:100%;
    bottom: 0;
}

.wrap {
  position: relative;
  height: 200px;
  width: 200px;
  background: green;
}
.box {
  background: yellow;
  position: absolute;
  bottom: 0;
  left: 0;
  height: 20px;
  transition: all 1s ease 0s;
}
.wrap:hover .box {
  height: 100%;
  bottom: 0;
}
<div class="wrap">
  <div class="box">
    Box
  </div>
</div>

关于html - CSS 过渡和自动定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28807945/

相关文章:

html - BeautifulSoup 找不到元素

html - 如何更改 Safari Web Apps 的 iOS 状态栏的背景颜色?

css - 尝试拥有可以通过 onclick 按钮事件应用的 CSS 样式

javascript - 一组 CSS 和 JS 文件在 HTML 中导入?

css - 如何让 liveserver 渲染 django 模板?

javascript - jquery no.conflict 不工作

html - 如何在浏览器窗口较小的情况下均匀间隔三张图片,并避免它们重叠?

html - 边框左上切

css - html和*在为整个页面设置颜色或字体时的区别

jquery - 带有 JQuery 的菜单中的 CSS Sprites 问题