Move div with dynamic height out of its parent container

后端 未结 2 1073
温柔的废话
温柔的废话 2021-01-26 09:50

I\'m trying to move a div with a dynamically changing height out of it\'s parent div and back in.

The problem is the dynamically height, otherwise I could easily set the

相关标签:
2条回答
  • 2021-01-26 10:07

    You could use CSS transform:translateY(100%) property, so the height is calculated based on the element itself. Then reset the value to 0 on hover.

    Inspect the element, you'll be able to see exact the height and position of it.

    Also take a look of support tables for transform, and prefix it if necessary.

    Updated JsFiddle

    .outer {
        position: relative;
        display: inline-block;
        width: 100px;
        height: 100px;
        background: grey;
        overflow: hidden;
    }
    .inner {
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        background: aqua;
        transition: 0.4s;
        transform: translateY(100%);
    }
    .outer:hover .inner {
        bottom: 0;
        transform: translateY(0);
    }
    <div class="outer">
        <div class="inner">Some expanding text here..</div>
    </div>

    0 讨论(0)
  • 2021-01-26 10:09

    If I understand your issue, you can set a max-height for its normal and :hover state and transition it. However, you must set it to a max-height that you know will always be tall enough (which may lead to random speeds depending on how much content there is).

    So something like: JS Fiddle

    .outer {
        position: relative;
        display: inline-block;
        width: 400px;
        height: 400px;
        background: black;
        overflow: hidden;
    }
    .inner {
        position: absolute;
        bottom: 0px;
        width: 100%;
        background: red;
        transition: 0.4s;
        max-height: 0;
        overflow: hidden;
    
    }
    .outer:hover .inner {
        transition: 0.4s;
        bottom: 0;
        max-height: 40px;
    }
    

    Otherwise, I would recommend a JS solution.

    0 讨论(0)
提交回复
热议问题