Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transition flex-grow of items in a flexbox

Is it possible to transition the items in an flexbox? When you click I want all items to collapse except the one that is clicked. The one that is clicked should use all available space from the container.

// only works once!  $(".item").click(function() {    $(".item").not(this).each(function() {      $(this).addClass("collapse");    });  });
html, body {    width: 100%;    height: 100%;    margin: 0;    padding: 0;    border: 0;    overflow: hidden;  }  .container {    flex-grow: 1;    flex-shrink: 0;    flex-basis: auto;    display: flex;    flex-direction: column;    height: 100%;  }  .item {    flex-grow: 1;    flex-shrink: 1;    flex-basis: auto;    transition: all 2s;  }  .collapse {    flex-grow: 0;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="container">    <div class="item" style="background: red">a</div>    <div class="item" style="background: green">b</div>    <div class="item" style="background: blue">c</div>    <div class="item" style="background: purple">d</div>  </div>

JSFiddle: http://jsfiddle.net/clankill3r/L8hktsgn/

like image 606
clankill3r Avatar asked Apr 14 '15 10:04

clankill3r


People also ask

What is Flex grow in flexbox?

The flex-grow property specifies how much the item will grow relative to the rest of the flexible items inside the same container. Note: If the element is not a flexible item, the flex-grow property has no effect.

How can you cause a flex item to grow in size?

flex: 1 0 200px; If you have one element that has a flex-basis of 200px, flex-grow of 1, and flex-shrink of 0, this element will be at minimum 200px wide, but it will be allowed to grow if there is extra space. In this case, you can think of the flex-basis as being a minimum width.

What flexbox property changes the order of the Flex items?

The order property and accessibility Use of the order property has exactly the same implications for accessibility as changing the direction with flex-direction . Using order changes the order in which items are painted, and the order in which they appear visually.

Can you nest a flexbox inside of a flexbox?

You can create two dimensional layouts by nesting a flex container inside another one. Flexbox is inherently a one dimensional layout model. Flex items within a flex container can be laid out either horizontally or vertically, but not both.


2 Answers

flex-grow is animatable but only if the value is a <number>. However it seems that Google Chrome (v41) doesn't trigger the animation if the value is set to 0.

A workaround for this could be to use a very small value instead — something like 0.0001:

Updated example.

$(".item").click(function() {      $(".item").addClass("collapse");      $(this).removeClass("collapse");      });
html, body {      width: 100%;      height: 100%;      margin: 0;      padding: 0;      border: 0;      overflow: hidden;      }    .container {      flex-basis: auto;      display: flex;      flex-direction: column;      height: 100%;  }    .item {      flex-grow: 1;      flex-shrink: 1;      flex-basis: auto;      transition: all 2s;  }    .collapse {      flex-grow: 0.001;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="container">      <div class="item" style="background: red">a</div>      <div class="item" style="background: green">b</div>      <div class="item" style="background: blue">c</div>      <div class="item" style="background: purple">d</div>  </div>
like image 61
Hashem Qolami Avatar answered Sep 24 '22 04:09

Hashem Qolami


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

and you can animate flex-grow from 20 to 1

.item {     flex-grow: 20;     transition: all 1s; }  .collapse {     flex-grow: 1;  } 

http://jsfiddle.net/L8hktsgn/11/

like image 37
Ashot Khanamiryan Avatar answered Sep 26 '22 04:09

Ashot Khanamiryan