css - 从容器底部到顶部的过渡 div 位置

标签 css css-position css-transitions

<分区>

我正在尝试让一个(绝对定位的)div 从 bottom: 0 过渡到它的父 div 的 top: 0。例如,我可以很高兴地让它从 bottom: 0 过渡到 bottom: 10rem,但不能过渡到 top: 0。有什么想法吗?请参阅下面的代码示例,并提前致谢!

        .container {
            position: relative;
            height: 20rem;
            width: 20rem;
            border: solid blue 0.05rem;
        }

        .child {
            position: absolute;
            display: inline-block;
            height: 5rem;
            width: 5rem;
            background-color: red;
            bottom: 0;
            top: auto;
            transition: all 1s;
        }

        .child:hover {
            top: 0;
            bottom: auto;
        }
<div class="container">
  <div class="child"></div>
</div>

最佳答案

使用calc() CSS function , 然后 :hover 父对象而不是 子对象

 .container {
            position: relative;
            height: 20rem;
            width: 20rem;
            border: solid blue 0.05rem;
        }

        .child {
            position: absolute;
            display: inline-block;
            height: 5rem;
            width: 5rem;
            background-color: red;
            top: calc( 100% - 5rem ); /*100% - height*/
            transition: all .3s ease;
            will-change: top;
        }

  .container:hover  .child{
            top: 0;  
        }
<div class="container">
  <div class="child"></div>
</div>

您还可以使用 The transform CSS property翻译子元素。

 .container {
            position: relative;
            height: 20rem;
            width: 20rem;
            border: solid blue 0.05rem;
        }

        .child {
            position: absolute;
            display: inline-block;
            height: 5rem;
            width: 5rem;
            background-color: red;
            bottom: 0;
            transition: all .3s ease;
            will-change: bottom;
        }

.container:hover  .child{
    bottom: 100%;
    transform: translateY(100%)
}
 
<div class="container">
  <div class="child"></div>
</div>

关于css - 从容器底部到顶部的过渡 div 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58247729/

相关文章:

javascript - css3 转换元素不触发事件 onClick

css - 传单簇组背景颜色未出现在 map 打印导出中

jquery - 在 jQuery 中使用第 n 个子选择器是否依赖于浏览器对 CSS3 的支持?

jquery - heroku 上的应用程序错误 - 在本地主机上工作正常

jquery - 在保持相对的同时向下滚动时使侧边栏变粘

css - 为什么将 `transform: perspective(1px) translateZ(0)` 应用于此 css 转换?

jquery - 如果 tbody 有多个子元素,请添加 show element1

html - 使用 HTML 和 CSS 在 float 图像列表项上放置文本?

css - 绝对位置弹出 Div 在 FullCalendar 中不显示

html - 如何让这个汉堡抽屉菜单向右滑动?