简体   繁体   English

flexbox 防止换行

[英]flexbox prevent wrapping of rows

I have the following snippet in which when you click one of the red boxes, its width doubles in size.我有以下代码段,当您单击其中一个红色框时,其宽度会加倍。 The functionality that I want to achieve is that when the width increases, the other boxes in that row shrink to stop the wrapping of a box to a new 3rd line.我想要实现的功能是,当宽度增加时,该行中的其他框缩小以停止将一个框包装到新的第 3 行。

 $('.card').click(function() { if ($('.card.selected')[0] != this) $('.card.selected').toggleClass('selected', false); $(this).toggleClass('selected'); })
 section { display: flex; flex: 1 1 100%; flex-wrap: wrap; flex-direction: row; justify-content: flex-start; width: 100%; } .card { display: block; width: calc(25% - 2px); height: 40px; background-color: #f00; margin: 1px; -webkit-transition: .15s; transition: width .15s; } .selected { width: calc(50% - 2px); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section> <div class="card"></div> <div class="card"></div> <div class="card"></div> <div class="card"></div> <div class="card"></div> <div class="card"></div> <div class="card"></div> <div class="card"></div> </section>

I am able to achieve the desired functionality by literally wrapping each row in individual flexboxs, however I want to know if there is a css approach which will not require this.我能够通过将每一行包装在单独的 flexbox 中来实现所需的功能,但是我想知道是否有不需要这样做的 css 方法。 The reason being that the boxes are being dynamically toggled on and off, and I want them to "flow" as you would expect and explicitly defining the rows makes this much more awkward.原因是盒子被动态地打开和关闭,我希望它们像你期望的那样“流动”,明确定义行使得这更加尴尬。

If you have 2 or maximum 3 rows, you can use pseudo :before and/or :after to force new lines.如果您有 2 行或最多 3 行,则可以使用伪:before和/或:after强制换行。 flex:1; should evenly dispatch the children on each rows.应该均匀地分派每一行的孩子。 You can also use a min-width to easily add a transition.您还可以使用min-width轻松添加过渡。

This technic allows you to draw 3 rows without extra markup.这种技术允许您在没有额外标记的情况下绘制 3 行。 to draw more rows, use a new container or insert elements to break the lines the same way the pseudo does.要绘制更多行,请使用新容器或插入元素以与伪代码相同的方式断开线条。

example with 2 rows 2 行示例

 section { display:flex; flex-wrap:wrap; } div { flex:1; min-width:calc(16.66% - 2px); transition:0.5s; margin:auto; height: 40px; background-color: #f00; margin: 1px; -webkit-transition: 0.15s; transition: width 0.15s; order:0; } div:nth-child(4)~div {/* every div starting from the fith */ order:2; } section:before{ content:''; width:100%; /* fill a whole row */ order:1;/* comes before the fith div*/ } div:focus {/* css demo purpose instead js onclick event */ min-width:calc(50% - 2px); transition:0.5s; background-color:yellow } div {display:flex;align-items:center;justify-content:center;} section {counter-reset: div} section div:before {counter-increment:div;content:'DIV N°: 'counter(div);}
 <section> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> </section>

example with 3 rows : 3 行示例:

 section { display:flex; flex-wrap:wrap; } div { flex:1; min-width:calc(16.66% - 2px); transition:0.5s; margin:auto; height: 40px; background-color: #f00; margin: 1px; -webkit-transition: 0.15s; transition: width 0.15s; order:0; } div:nth-child(4)~div { order:2; } div:nth-child(8)~div { order:4; } section:before, section:after{ content:''; width:100%; order:1; } section:after { order:3; } div:focus { min-width:calc(50% - 2px); transition:0.5s; background-color:yellow } div {display:flex;align-items:center;justify-content:center;} section {counter-reset: div} section div:before {counter-increment:div;content:'DIV N°: 'counter(div);}
 <section> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> <div tabindex="0"></div> </section>

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

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