弹性过渡:按内容拉伸(或缩放)适应

9
我编写了一个脚本(在这里得到了用户的帮助),它允许我扩展所选的div,并使其他div相应地拉伸以适应剩余空间(除第一个div外,其宽度是固定的)。
这是我想要实现的效果的图片:

enter image description here

为此,我使用了flex和transitions。
它工作得很好,但是jQuery脚本指定了一个“400%”的拉伸值(这对测试非常好)。
现在,我想让所选的div扩展/缩小以完全适合内容,而不是“400%”的固定值。
我不知道该怎么做。
这可能吗?
我尝试克隆div,将其调整到内容大小,获取其值,然后使用此值进行过渡,但这意味着我有一个百分比的初始宽度,但是目标值是像素。那不起作用。
如果我将像素值转换为百分比,则结果由于某种原因并不完全适合内容。
在所有情况下,这似乎是实现我想要的方式有点复杂。
难道没有任何flex属性可以过渡以适应所选div的内容吗?
以下是代码(编辑/简化后以获得更好的阅读体验):

var expanded = '';

$(document).on("click", ".div:not(:first-child)", function(e) {

  var thisInd =$(this).index();
  
  if(expanded != thisInd) {
    //fit clicked fluid div to its content and reset the other fluid divs 
    $(this).css("width", "400%");
    $('.div').not(':first').not(this).css("width", "100%");
    expanded = thisInd;
  } else {
    //reset all fluid divs
    $('.div').not(':first').css("width", "100%");
    expanded = '';
  }
});
.wrapper {
  overflow: hidden;
  width: 100%;
  margin-top: 20px;
  border: 1px solid black;
  display: flex;
  justify-content: flex-start;
}

.div {
  overflow: hidden;
  white-space: nowrap;
  border-right: 1px solid black;
  text-align:center;
}

.div:first-child {
  min-width: 36px;
  background: #999;
}

.div:not(:first-child) {
  width: 100%;
  transition: width 1s;
}

.div:not(:first-child) span {
  background: #ddd;
}

.div:last-child {
  border-right: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Click on the div you want to fit/reset (except the first div)

<div class="wrapper">

  <div class="div"><span>Fixed</span></div>
  <div class="div"><span>Fluid (long long long long long text)</span></div>
  <div class="div"><span>Fluid</span></div>
  <div class="div"><span>Fluid</span></div>

</div>

这是 jsfiddle:

https://jsfiddle.net/zajsLrxp/1/

编辑:在大家的帮助下,这是我的可行解决方案(在窗口大小调整时更新大小+动态计算div数量和第一列的宽度):

var tableWidth;
var expanded    = '';
var fixedDivWidth  = 0;
var flexPercentage = 100/($('.column').length-1);

$(document).ready(function() {

    // Set width of first fixed column
    $('.column:first-child .cell .fit').each(function() {
        var tempFixedDivWidth = $(this)[0].getBoundingClientRect().width;
        if( tempFixedDivWidth > fixedDivWidth ){fixedDivWidth = tempFixedDivWidth;}
    });
    $('.column:first-child'  ).css('min-width',fixedDivWidth+'px')
 
    //Reset all fluid columns
    $('.column').not(':first').css('flex','1 1 '+flexPercentage+'%')
})

$(window).resize( function() {

    //Reset all fluid columns
    $('.column').not(':first').css('flex','1 1 '+flexPercentage+'%') 
    expanded   = '';
})

$(document).on("click", ".column:not(:first-child)", function(e) {
 
    var thisInd =$(this).index(); 
 
    // if first click on a fluid column
    if(expanded != thisInd) 
    {
        var fitDivWidth=0;
    
        // Set width of selected fluid column
        $(this).find('.fit').each(function() {
            var c = $(this)[0].getBoundingClientRect().width;
            if( c > fitDivWidth ){fitDivWidth = c;}
        });
        tableWidth = $('.mainTable')[0].getBoundingClientRect().width; 
        $(this).css('flex','0 0 '+ 100/(tableWidth/fitDivWidth) +'%')
  
        // Use remaining space equally for all other fluid column
        $('.column').not(':first').not(this).css('flex','1 1 '+flexPercentage+'%')
  
        expanded = thisInd;
    }

    // if second click on a fluid column
    else
    {
        //Reset all fluid columns
        $('.column').not(':first').css('flex','1 1 '+flexPercentage+'%')

        expanded = '';
    }
  
});
body{
    font-family: 'Arial';
    font-size: 12px;
    padding: 20px;
}

.mainTable {
    overflow: hidden;
    width: 100%;
    border: 1px solid black;
    display: flex;
    margin-top : 20px;
}

.cell{
    height: 32px;
    border-top: 1px solid black;
    white-space: nowrap;
}

.cell:first-child{
    background: #ccc;
    border-top: none;
}

.column {
    border-right: 1px solid black;
    transition: flex 0.4s;
    overflow: hidden;
    line-height: 32px;
    text-align: center;
}

.column:first-child {
    background: #ccc;
}

.column:last-child {
  border-right: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<span class="text">Click on the header div you want to fit/reset (except the first one which is fixed)</span>

<div class="mainTable">
    <div class="column">
        <div class="cell"><span class="fit">Propriété</span></div>
        <div class="cell"><span class="fit">Artisan 45</span></div>
        <div class="cell"><span class="fit">Waterloo 528</span></div>     
    </div>
    
    <div class="column">     
        <div class="cell"><span class="fit">Adresse</span></div>
        <div class="cell"><span class="fit">Rue du puit n° 45 (E2)</span></div>
        <div class="cell"><span class="fit">Chaussée de Waterloo n° 528 (E1)</span></div>     
    </div>
  
    <div class="column">     
        <div class="cell"><span class="fit">Commune</span></div>
        <div class="cell"><span class="fit">Ixelles</span></div>
        <div class="cell"><span class="fit">Watermael-Boitsfort</span></div>     
    </div>
      
    <div class="column">     
        <div class="cell"><span class="fit">Ville</span></div>
        <div class="cell"><span class="fit">Marche-en-Famenne</span></div>
        <div class="cell"><span class="fit">Bruxelles</span></div>     
    </div>
    
    <div class="column">
        <div class="cell"><span class="fit">Surface</span></div>
        <div class="cell"><span class="fit">120 m<sup>2</sup></span></div>
        <div class="cell"><span class="fit">350 m<sup>2</sup></span></div>     
    </div>
</div>

这里是一个完整的示例,包括样式、填充和更多数据:

https://jsfiddle.net/zrqLowx0/2/

谢谢大家!


1
请友善地在您的问题中添加一个“最终结果”,展示您在所有这些之后所创建的内容。我真的很想知道它的样子。谢谢!(也许可以提供一些关于它用途的见解?) - Rene van der Lende
好的,我会尽快清理脚本中所有不必要的元素(我想几分钟就可以了)。那么,我该怎么做呢?我只需编辑原帖并添加我的看法吗,即使Azametzin的答案已经很好了?我改了一些东西,但它的90%是相同的(我很乐意展示它).. - Bachir Messaouri
这里的重点是:我模拟了一种类似Excel电子表格的东西,希望人们能够点击特定的列来适应内容。通常,一个表格就可以完成,但是当涉及到调整大小时,列并没有按照我想要的方式进行。虽然很容易单独扩展一列,但是很难使其他列实时地按照flex对divs的方式占用剩余空间。它们只会不规则地扩展。我做了一些研究,发现这是使用表格不可能实现的。还有其他原因我不使用表格或dataTable,但这超出了本篇文章的范围。 - Bachir Messaouri
1
足够简单,只需点击“编辑”,转到问题的末尾并开始添加一些散文式的思考,包括一个新的代码片段(全部)。确保默认情况下“隐藏代码片段”(左下角复选框),对于“新读者”来说不那么显眼/分散注意力。我的第一个想法是你要用它来制作一些花哨的“口琴”样式的小部件,带有图标等等。电子表格啊,我有一些使用相同原理讨论在这里的全屏幕、上下左右拉伸的东西(如果你感兴趣的话)。 - Rene van der Lende
2
太棒了,你走了额外的一英里,发布了最终结果。这让在SO上回答问题更加有益和有意义!赞一个... - Rene van der Lende
显示剩余2条评论
4个回答

4

可以使用 max-widthcalc() 来解决这个问题。

首先,将 CSS 中 div 的 width: 100% 替换为 flex: 1,这样它们就会自动增长,这在这种情况下更好。此外,为 max-width 使用过渡效果。

现在,我们需要存储一些相关值:

  • 将被动画的 div 数量(divsLength 变量) - 在本例中为 3。
  • 用于固定 div 和边框的总宽度(extraSpace 变量) - 在本例中为 39px。

有了这两个变量,我们就可以为所有的 div 设置一个默认的 max-widthdefaultMaxWidth 变量),以及在以后使用它们。这就是为什么它们被全局存储的原因。

defaultMaxWidthcalc((100% - extraSpace)/divsLength)

现在,让我们进入点击函数:

要展开 div,目标文本的宽度将存储在一个名为 textWidth 的变量中,并将其作为 max-width 应用于 div。它使用 .getBoundingClientRect().width(因为它返回浮点数值)。

对于其余的 div,为它们创建了一个 calc()max-width,将应用于它们。
这是:calc(100% - textWidth - extraScape)/(divsLength - 1)
计算结果是每个剩余 div 应该有的宽度。

在单击展开的 div 后,即返回到正常状态时,会再次将默认的 max-width 应用于所有的 .div 元素。

var expanded = false,
    divs = $(".div:not(:first-child)"),
    divsLength = divs.length,
    extraSpace = 39, //fixed width + border-right widths 
    defaultMaxWidth = "calc((100% - " + extraSpace + "px)/" + divsLength + ")";

    divs.css("max-width", defaultMaxWidth);

$(document).on("click", ".div:not(:first-child)", function (e) {
  var thisInd = $(this).index();

  if (expanded !== thisInd) {
    
    var textWidth = $(this).find('span')[0].getBoundingClientRect().width;  
    var restWidth = "calc((100% - " + textWidth + "px - " + extraSpace + "px)/" + (divsLength - 1) + ")";
    
    //fit clicked fluid div to its content and reset the other fluid divs 
    $(this).css({ "max-width": textWidth });
    $('.div').not(':first').not(this).css({ "max-width": restWidth });
    expanded = thisInd;
  } else {
    //reset all fluid divs
    $('.div').not(':first').css("max-width", defaultMaxWidth);
    expanded = false;
  }
});
.wrapper {
  overflow: hidden;
  width: 100%;
  margin-top: 20px;
  border: 1px solid black;
  display: flex;
  justify-content: flex-start;
}

.div {
  overflow: hidden;
  white-space: nowrap;
  border-right: 1px solid black;
  text-align:center;
}

.div:first-child {
  min-width: 36px;
  background: #999;
}

.div:not(:first-child) {
  flex: 1;
  transition: max-width 1s;
}

.div:not(:first-child) span {
  background: #ddd;
}

.div:last-child {
  border-right: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

Click on the div you want to fit/reset (except the first div)

<div class="wrapper">

  <div class="div"><span>Fixed</span></div>
  <div class="div"><span>Fluid (long long long long text)</span></div>
  <div class="div"><span>Fluid</span></div>
  <div class="div"><span>Fluid</span></div>

</div>

这种方法具有动态性,应该适用于任何分辨率。
你需要硬编码的唯一值是extraSpace变量。


1
是的,这是一个需要精确计算和更深入理解flexbox shrink效果的问题。我目前还没有“完美”的解决方案,但我希望将来能再次尝试,因此我会收藏您的问题以便在另一个时间尝试弄清如何正确处理它。这是一个很好的挑战。 - Azametzin
1
好的!请慢慢来,反正我也出去几个小时了;-) - Bachir Messaouri
1
哦,我刚看到你编辑的评论(本来期待一个更新的评论)。使用getBoundingClientRect()非常聪明,这个想法没出现在我的脑海中(同样适用于考虑39像素固定宽度)。我会进行更多测试,以查看它是否适用于任何窗口大小和任意数量的Fluid divs。我会回复你的。非常感谢! PS:你的解决方案可以在jQuery 3.3.1上运行,但不能在3.4.1上运行,原因不明。 - Bachir Messaouri
1
我遇到了各种窗口大小和div数量的问题。下面的另一个脚本可以适应内容,但无法正确调整其他div的大小。为了解决这个问题,我必须回到你的脚本。一旦我使用了你的两个脚本,奇怪的是,两者的混合似乎可以起作用。我正在测试,完成后会再联系你。 - Bachir Messaouri
1
我无法复现您的意思。您能否给我一个片段作为HTML结构失败的示例? - Azametzin
显示剩余7条评论

3

您需要处理宽度或计算函数。Flexbox会有解决方案。

要使所有的div相等(除第一个外),我们使用flex: 1 1 auto

<div class="wrapper">
  <div class="div"><span>Fixed</span></div>
  <div class="div"><span>Fluid (long long long long text)</span></div>
  <div class="div"><span>Fluid</span></div>
  <div class="div"><span>Fluid</span></div>
</div>

为普通 div 和选中的 div 定义 flex 规则。使用 transition: flex 1s; 来帮助你实现。对于选中的 div,我们不需要 flex grow,所以使用 flex: 0 0 auto

.wrapper {
  width: 100%;
  margin-top: 20px;
  border: 1px solid black;
  display: flex;
}

.div {
  white-space: nowrap;
  border-right: 1px solid black;
  transition: flex 1s;
  flex: 1 1 auto;
}

.div.selected{
  flex: 0 0 auto;
}

.div:first-child {
  min-width: 50px;
  background: #999;
  text-align: center;
}

.div:not(:first-child) {
  text-align: center;
}

.div:last-child {
  border-right: 0px;
}

div:not(:first-child) span {
  background: #ddd;
}

每次用户点击div时,都会添加所选类。您还可以在第二次单击时使用toggle,以便将所选项目保存在映射中,并且可以显示多个所选项目(当然不是使用此代码示例)。
$(document).on("click", ".div:not(:first-child)", function(e) {
  const expanded = $('.selected');
  $(this).addClass("selected");
  if (expanded) {
    expanded.removeClass("selected");
  }
});

https://jsfiddle.net/f3ao8xcj/


谢谢。我需要研究一下你的情况,因为它与我想要的相反,我还不确定如何扭转这种情况(也许这很简单)。 默认情况下,我希望所有流体div大小相等。只有当我点击其中一个时,该div才适应其内容(其他流体div平均分享剩余空间)。 如果我再次点击同一个div,所有Fluid div将重置,再次平均分享空间,不考虑它们的内容。 所以,如果我没弄错的话,这正好与你所做的相反。还不确定如何扭转这种情况。 - Bachir Messaouri
然而,如果这可以被反转,我真的很喜欢你的解决方案优雅和简单的方式,因为不需要去处理CSS宽度属性。Flex似乎只是在内部处理这个问题。话虽如此,我有一种预感这将更难以实现相反的方法。等着瞧吧。感谢您的跟进。 - Bachir Messaouri
1
@BachirMessaouri 这很简单。请查看更新版本。 - hurricane
我喜欢你脚本的优雅,但是你的解决方案只考虑到我的请求部分。我的请求不仅仅是关于适应内容,还包括在点击流动div之前、期间和之后均匀地分享剩余空间。你的脚本对此处理得不太好,甚至可以说没有处理。这就是使整个问题成为一个难题的原因。我在原帖中添加了一张图片,以清楚地说明我的需求。上面的另一个脚本正是从你无法实现的地方开始的。奇怪的是,似乎将你的脚本和上面的脚本混合使用可能会起作用。我正在测试,测试完成后会回复你。 - Bachir Messaouri
@BachirMessaouri 这里你缺少的是,没有人需要为你的情况提供完美的解决方案。Flex transition: Stretch (or shrink) to fit content 是你的问题标题,而这个答案就是如何进行简单示例。找到解决方案是你的责任。 - hurricane
我认为你忽略了一个问题,那就是你的解决方案在另一个解决方案开始时结束。这意味着当你的解决方案没有考虑到问题的其他方面时,我们正好遇到问题的初始部分。这是第一点。 第二点是它现在是一个悬赏任务。因为我正在寻找符合我的要求的解决方案,所以我会投入一些声望。理论上的解决方案并不是真正的解决方案,而只是一个概念。 如果我需要获得被同样大的div包围的适合文本,那么只有一半的解决方案并不能算作解决方案。 - Bachir Messaouri

2
经过几次试验,这似乎是我最简短、最直接的解决方案。
基本上只需要让Flexbox默认将
元素拉伸到其极限,但当点击时,将
的拉伸约束为的宽度...
伪代码:
被点击并且已经切换状态,则
的最大宽度=100%,重置的切换状态
否则,
的最大宽度= 的宽度,设置的切换状态
我将CSS分成了“相关机制”和“仅用于视觉效果”的部分,以便阅读(和代码重用)。
代码有很多注释,所以这里没有太多的文字...
问题:在将
从max-width: 100%转换为max-width = span width时,某种情况下会出现额外的延迟。我已经在Chrome、Edge、IE11和Firefox中检查了这种行为(都是W10),所有浏览器都似乎有这个怪癖。可能是一些浏览器内部重新计算的问题,或者可能是过渡时间使用了两次(“感觉像”)。反之,奇怪的是,没有额外的延迟。
然而,使用短的过渡时间(例如150ms,就像我现在使用的那样),这种额外的延迟是不/几乎不可感知的。(另一个SO问题的好例子...)

$(document).on('click', '.wrapper>:not(.caption) span', function (e) {
// Save the current 'toggle' status
var elemToggled = e.target.getAttribute('toggled');

   // Set parent max-width to maximum space or constraint to current child width
    e.target.parentElement.style.maxWidth = 
        (elemToggled=="true") ? '100%' : parseFloat(window.getComputedStyle(e.target).width) + 'px';
    
    // (Re)set child toggle state 
    e.target.setAttribute('toggled', (elemToggled=="true") ? false : true);    
});
/*********************/
/* Wrapper mechanism */
/*********************/
.wrapper { /* main flexible parent container */
    display  : flex;        /* [MANDATORY] Flexbox Layout container, can't FBL without */
    flex-wrap: nowrap;      /* [MANDATORY] default FBL, but important. wrap to next line messes things up */
    flex-grow: 1;           /* [OPTIONAL] Either: if '.wrapper' is a FBL child itself, allow it to grow */
    width    : 100%;        /* [OPTIONAL] or    : full parent width */
                            /* (Maybe a fixed value, otherwise redundant here as 'flex-grow' = 1) */
}
/* generic rule */
.wrapper>* { /* flexed child containers, being flexible parent containers themselves */
    flex-grow : 1;          /* [MANDATORY] important for this mechanism to work */
    overflow: hidden;       /* [MANDATORY] important, otherwise output looks messy */

    display: flex;          /* [MANDATORY] for FBL stretching */
    justify-content: center;/* [MANDATORY] as per SOQ */

    max-width : 100%;       /* [OPTIONAL/MANDATORY], actually needed to trigger 'transition' */
}
/* exception to the rule */
.wrapper>.fixed { /* fixed child container */
    flex-grow: 0;           /* [MANDATORY] as per SOQ, don't allow grow */
}

/******************/
/* Eye-candy only */
/******************/
.wrapper {
    border: 1px solid black;
}
.wrapper>:not(.fixed) {
    transition: max-width 150ms ease-in-out; 
}
.wrapper>:not(:last-child){
    border-right: 1px solid black;
}
/* generic rule */
.wrapper>*>span {
    white-space: nowrap;
    background-color: #ddd;
}
/* exception to the rule */
.wrapper>.fixed>span {
    background-color: #999;
}

/* debug helper: show all elements with outlines (put in <body>) */
[debug="1"] * { outline: 1px dashed purple }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
    <div class="fixed"><span>Fixed</span></div>
    <div><span>Fluid (long long long long long text)</span></div>
    <div><span>Fluid</span></div>
    <div><span>Fluid</span></div>
</div>

更新

新版本重置了所有其他的<div>。我真的很讨厌这种跳动感,但这是由于Flexbox的拉伸和transition值造成的。如果没有transition,就看不到跳动。您需要尝试一下哪种方法适合您。

我只在javascript代码中添加了document.querySelectorAll()

$(document).on('click', '.wrapper>:not(.caption) span', function (e) {
var elemToggled = e.target.getAttribute('toggled');                    // Toggle status
var elemWidth   = parseFloat(window.getComputedStyle(e.target).width); // Current element width

    // reset ALL toggles but 'this'...
    document.querySelectorAll('.wrapper>:not(.caption) span')
        .forEach( function (elem,idx) {
                      if (elem != this){
                         elem.parentElement.style.maxWidth = '100%';
                         elem.setAttribute('toggled',false);
                      };
                  });

    // Set parent max-width to maximum space or constraint to current child width
    e.target.parentElement.style.maxWidth = 
        (elemToggled=="true") ? '100%' : parseFloat(window.getComputedStyle(e.target).width) + 'px';

    // (Re)set child toggle state 
    e.target.setAttribute('toggled', (elemToggled=="true") ? false : true);    
});
/*********************/
/* Wrapper mechanism */
/*********************/
.wrapper { /* main flexible parent container */
    display  : flex;        /* [MANDATORY] Flexbox Layout container, can't FBL without */
    flex-wrap: nowrap;      /* [MANDATORY] default FBL, but important. wrap to next line messes things up */
    flex-grow: 1;           /* [OPTIONAL] Either: if '.wrapper' is a FBL child itself, allow it to grow */
    width    : 100%;        /* [OPTIONAL] or    : full parent width */
                            /* (Maybe a fixed value, otherwise redundant here as 'flex-grow' = 1) */
}
/* generic rule */
.wrapper>* { /* flexed child containers, being flexible parent containers themselves */
    flex-grow : 1;          /* [MANDATORY] important for this mechanism to work */
    overflow: hidden;       /* [MANDATORY] important, otherwise output looks messy */

    display: flex;          /* [MANDATORY] for FBL stretching */
    justify-content: center;/* [MANDATORY] as per SOQ */

    max-width : 100%;       /* [OPTIONAL/MANDATORY], actually needed to trigger 'transition' */
}
/* exception to the rule */
.wrapper>.fixed { /* fixed child container */
    flex-grow: 0;           /* [MANDATORY] as per SOQ, don't allow grow */
}

/******************/
/* Eye-candy only */
/******************/
.wrapper {
    border: 1px solid black;
}
.wrapper>:not(.fixed) {
    transition: max-width 150ms ease-in-out; 
}
.wrapper>:not(:last-child){
    border-right: 1px solid black;
}
/* generic rule */
.wrapper>*>span {
    white-space: nowrap;
    background-color: #ddd;
}
/* exception to the rule */
.wrapper>.fixed>span {
    background-color: #999;
}
/* show all elements with outlines (put in <body>) */
[debug="1"] * { outline: 1px dashed purple }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="wrapper">
    <div class="fixed"><span>Fixed</span></div>
    <div><span>Fluid (long long long long long text)</span></div>
    <div><span>Fluid</span></div>
    <div><span>Fluid</span></div>
</div>


非常感谢您的提议。它不符合我的需求(但是这是一个非常好的起点),因为当我们单击流体div时,它适合得很好,但是另外两个div不会平均扩展以占用剩余空间,这是问题的50%。是的,还有这种古怪的情况。Azametzin的脚本完美地完成了工作,没有任何古怪的情况。我最终混合了他的脚本和使用flex转换而不是max-width,现在它运行得非常好。非常感谢您的帮助! - Bachir Messaouri
再次支持@Azametzin的评论:这是一个非常有趣的故事。:). 下次也许可以从图片开始? - Rene van der Lende
好的。我以为解释已经足够了,但显然我错了。不过那张图片从昨天就在那里了。但我明白你的意思。我会在几分钟内编辑我的原始消息,展示我的自定义脚本。非常感谢! - Bachir Messaouri

0
如果您只需要一行,可以基于以下代码的简单解决方案:https://jsfiddle.net/jpeter06/a5cu52oy/,其中CSS flex已修改为列而不是行。
.container {
    flex-grow: 10;
    flex-shrink: 0;
    flex-basis: auto;
    display: flex;
    flex-direction: row;
    width: 100%;
}
.item { min-width:30px;
        flex-basis:30px;
        overflow-x:hidden;
        transition: flex-basis 500ms ease-in-out;
}
.expanded {
   flex-basis: 20em;
}
html, body {
    width: 100%;    height: 100%;
    margin: 0; padding: 0; border: 0; overflow: hidden;  
}

HTML代码:

<div class="container">
    <div class="item" style="background: red">a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/></div>
    <div class="item" style="background: green">b<br/>b<br/>b<br/>b</div>
    <div class="item" style="background: blue">c<br/>c<br/>c<br/>c</div>
</div>

JS 代码:

$(document).ready(function() {
  $(".item").click(function() {
    $(this).addClass('expanded');
    $(".item").not(this).each(function() {
        $(this).removeClass("expanded");    
    });
  });
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接