简体   繁体   English

删除一项后保留弹性项目

[英]Keep flex items in place after deleting one item

I have the following html:我有以下 html:

<div class="grommetux-box grommetux-box--direction-row grommetux-box--align-center grommetux-box--responsive grommetux-box--pad-none grommetux-box--wrap second-header">
    <div class="grommetux-box grommetux-box--direction-row grommetux-box--responsive grommetux-box--pad-none school-info">
        <header class="grommetux-box grommetux-box--direction-row grommetux-box--align-center grommetux-box--pad-horizontal-none grommetux-box--pad-vertical-none grommetux-box--pad-between-small grommetux-header school-name">
            <!-- react-text: 74 --><!-- /react-text -->
        </header>
        <header class="grommetux-box grommetux-box--direction-row grommetux-box--align-center grommetux-box--pad-horizontal-none grommetux-box--pad-vertical-none grommetux-box--pad-between-small grommetux-header grommetux-header--small school-code">
            <!-- react-text: 76 -->School Code: <!-- /react-text -->
        </header>
    </div>
    <div class="grommetux-box grommetux-box--direction-row grommetux-box--responsive grommetux-box--pad-none search-box">

    </div>
</div>

My CSS我的 CSS

.grommetux-box {
    display: -ms-flexbox;
    display: flex;
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;

}

.grommetux-box--direction-row {
    -ms-flex-direction: row;
    flex-direction: row;
}

.search-box {
    text-decoration: none;
    justify-content: flex-end;
}

.school-info {
    text-indent: inherit;
    flex-direction: inherit;
    justify-content: flex-start;
    display: flex;
}

Sorry this code doesn't show the whole picture but basically my issue is when I delete the first inner div, the second inner div replaces it by taking its place (moving left).抱歉,这段代码没有显示整个画面,但基本上我的问题是当我删除第一个内部 div 时,第二个内部 div 将其替换为它的位置(向左移动)。 How can I design my flex so that if I delete one, it doesn't effect the position of the other?如何设计我的 flex,以便如果我删除一个,它不会影响另一个的 position?

In case, there are multiple items inside a flex-container and we remove one or more items, browser will recalculate and update the position of rest of the items automatically. 如果灵活容器内有多个项目,我们删除一个或多个项目,浏览器将重新计算并自动更新其余项目的位置。

There is one possibility though ie instead of removing items from the DOM we just hide them from the view with visibility: hidden . 虽然有一种可能性,即不是从DOM中删除项目,我们只是将它们隐藏在视图中, visibility: hidden This css property hides the elements from view but keep them in DOM. 此css属性隐藏视图中的元素,但将它们保存在DOM中。 As a result elements with such style will be hidden but keep the space in view. 因此,具有此类样式的元素将被隐藏,但保持可视空间。

A quick demo is in below snippet: 快速演示在以下片段中:

 .container { min-height: 200px; display: flex; } .item { background: blue; margin: 10px; width: 15%; } .item.hidden { visibility: hidden; } 
 <div class="container"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item hidden"></div> <div class="item"></div> </div> 

Here's another trick that might be helpful in some cases.这是另一个在某些情况下可能有用的技巧。 In case you want to keep certain flexbox items in their place while you add/remove others, you can use the visibility and height properties, such that you set the height of the 'hidden' item to 0 and only adjust it's visibility property to show/hide.如果您想在添加/删除其他项目时将某些 flexbox 项目保留在其位置,您可以使用visibilityheight属性,例如将“隐藏”项目的height设置为 0,并且仅调整其visibility属性以显示/隐藏。

I do not know why this works exactly, but it does.我不知道为什么这完全有效,但确实如此。 Maybe someone more knowledgeable can shed light on this as well as its limitations.也许更有知识的人可以阐明这一点及其局限性。 Upside is it does not use absolute positioning;好处是它不使用绝对定位; it displays immediately after other flexbox items.它立即显示在其他 flexbox 项目之后。

A useful use case would be to show an error message text without disrupting the flow of existing flexbox items.一个有用的用例是在不中断现有 flexbox 项目的流程的情况下显示错误消息文本。

 document.querySelector('#btn').addEventListener('click', function() { let additional_item = document.querySelector('.additional-item') if (additional_item.style['visibility'] == 'visible') { document.querySelector('.additional-item').style['visibility'] = 'hidden' } else { document.querySelector('.additional-item').style['visibility'] = 'visible' } })
 .container { height: 150px; width: 250px; background-color: lightgreen; margin-bottom: 15px; gap: 15px; display: flex; justify-content: center; flex-direction: column; }.flex-item { border: 1px solid black; text-align: center; }.additional-item { visibility: hidden; height: 0px; text-align: center; } #btn { width: 250px; }
 <div class="container"> <div class="flex-item">Two flexbox items</div> <div class="flex-item">perfectly positioned in the center</div> <div class="additional-item">Item that does not move other items</div> </div> <button id="btn">Click to show/hide additional item</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM