Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to animate Flexbox inserts & removes?

When I remove an item from a flexbox, the remaining items "snap" into their new positions immediately rather than animating.

Conceptually, since the items are changing their positions, I would expect the transitions to apply.

I have set the transition property on all involved elements (the flexbox and the children)

Is there any way to animate edits (adds & deletes) to a flexbox? This is actually a showstopper for me and the one missing piece with flexbox.

like image 433
mmaclaurin Avatar asked Jun 19 '12 18:06

mmaclaurin


People also ask

Can you animate Flex?

Animations are fun to use and implement in your web project. In advanced CSS, we can use flexbox for animation purposes. Flexbox container and Flexbox item properties are used in flex animation with keyframes and CSS techniques for creating flexbox animation.

How do you make a flexbox wrap?

Making things wrap If you want to cause them to wrap once they become too wide you must add the flex-wrap property with a value of wrap , or use the shorthand flex-flow with values of row wrap or column wrap . Items will then wrap in the container.

Should I use flexbox everywhere?

You need a content-first design : Flexbox is the ideal layout system to create web pages if you don't know exactly how your content is going to look, so if you want everything just to fit in, flexbox is perfect for that.


2 Answers

I've fixed up @skyline3000's demo based on this example from Treehouse. Not sure if this will break again if browsers change but this seems to be the intended way to animate flex size changes:

http://jsfiddle.net/2gbPg/2/

Also I used jQuery but it technically isn't required.

.flexed {     background: grey;     /* The border seems to cause drawing artifacts on transition. Probably a browser bug. */     /* border: 1px solid black; */     margin: 5px;     height: 100px;     flex-grow: 1;     transition: flex-grow 1000ms linear; }  .removed {     /* Setting this to zero breaks the transition */     flex-grow: 0.00001; } 

One thing to note about the CSS is you can't transition to a flex-grow of zero, it won't transition it will just disappear. You need to just put a very small value. Also there seems to be an artifacting bug when drawing borders so I've used a background in this case.

like image 109
Chris Nicola Avatar answered Sep 20 '22 02:09

Chris Nicola


Remember that the Flexible Box Model and Grid Layout specifications are changing constantly, even the properties and valid values. The browser implementations are far from complete as well. That being said, you can transition on the flex property so that the elements transition smoothly, then just listen for TransitionEnd to finally remove the node from the DOM tree.

Here is an example JSFiddle, running in Chrome 21: http://jsfiddle.net/5kJjM/ (click the middle div)

var node = document.querySelector('#remove-me');    node.addEventListener('click', function(evt) {    this.classList.add('clicked');  }, false);    node.addEventListener('webkitTransitionEnd', function(evt) {    document.querySelector('#flexbox').removeChild(this);  }, false);
#flexbox {    display: -webkit-flex;    -webkit-flex-flow: row;  }    .flexed {    border: 1px solid black;    height: 100px;    -webkit-flex: 1 0 auto;    -webkit-transition: -webkit-flex 250ms linear;  }    .clicked {    -webkit-flex: 0 0 auto;  }
<div id="flexbox">    <div class="flexed"></div>    <div class="flexed" id="remove-me"></div>    <div class="flexed"></div>  </div>

Edit: To further clarify, when you remove a node, you should set its flex to 0, then remove it from the DOM. When adding a node, add it in with flex: 0, then transition it to flex:1

like image 34
skyline3000 Avatar answered Sep 20 '22 02:09

skyline3000