<script src="http://fonts.googleapis.com/css?family=Ubuntu:400,700,400italic,700italic"></script>
<script src="https://rawgit.com/shyiko/lorem/master/src/library/lorem.js"></script>
Single line flex box smooth animated inserts and removes:
<div class="flexbox">
<div class="flex-item hover-collapse-item">I will collapse on hover</div>
<div class="flex-item">
this is some long texte this is some long texte this is some long texte this is some long texte this is some long texte<br>
this is some long texte
</div>
</div>
<button class="add">Add</button>
<button class="remove">Remove</button>
CSS
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html, body
{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body
{
background: #666666;
font-family: 'Ubuntu';
font-size: 14px;
}
.flexbox {
display: flex;
flex-direction: row;
border: 1px solid #000000;
}
.flex-item
{
height: 100px;
flex: 1;
overflow: hidden;
background: rgba(0, 0, 0, .3);
transition: all 300ms linear;
}
.flex-item:first-child
{
border-left: none;
}
.flex-item:nth-child(2)
{
border-left: none;
background: orange;
flex: .00001;
}
.hover-collapse-item
{
white-space: nowrap;
background: rgba(150, 0, 150, .3);
flex: 1;
text-align: center;
self-align: center;
align: center;
}
.flexbox:hover .hover-collapse-item
{
/* 0 does not work so we have to use a small number */
flex: .00001;
flex: .5;
}
.flexbox:hover .flex-item:nth-child(2)
{
/* 0 does not work so we have to use a small number */
flex: .5;
}
.new-item
{
/* 0 does not work so we have to use a small number */
/* Start our small */
flex: .00001;
background: rgba(0, 0, 0, .4);
-webkit-animation: flexGrow 500ms ease forwards;
-o-animation: flexGrow 500ms ease forwards;
animation: flexGrow 500ms ease forwards;
}
.remove-item
{
flex: 1;
-webkit-animation: flexShrink 500ms ease forwards;
-o-animation: flexShrink 500ms ease forwards;
animation: flexShrink 500ms ease forwards;
}
@-webkit-keyframes flexGrow {
to {
flex: 1;
}
}
@-o-keyframes flexGrow {
to {
flex: 1;
}
}
@keyframes flexGrow {
to {
flex: 1;
}
}
@-webkit-keyframes flexShrink {
to {
flex: .01;
flex: .00001;
}
}
@-o-keyframes flexShrink {
to {
flex: .01;
flex: .00001;
}
}
@keyframes flexShrink {
to {
flex: .01;
flex: .00001;
}
}
JavaScript
$('.add').on('click', function() {
$('.flexbox').append('<div class="flex-item new-item">' + lorem.ipsum('w4') + '</div>');
});
$('.remove').on('click', function() {
// Remove the last item in the list that isn't already being removed
var $item = $('.flexbox .flex-item:not(.remove-item)').last();
$item.addClass('remove-item');
$item.on('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd', function() {
$item.remove();
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.