Is it possible to animate position of flex items when items are removed/added?
I don't know much about angularJS, but you could do this:
http://jsfiddle.net/H9mvd/5/
using transitions. To remove an element, first you'd change the width, margins etc. to 0, and then remove the item on the 'transitionEnd' event:
$(this).css({
'margin-left': '0',
'margin-right': '0',
width: '0'
}).on('transitionend', function(){
$(this).remove();
});
And for adding elements, insert the new element with the style attribute such that width, margins etc. are 0. Then remove these from style
so that the element gets transitioned to it's proper size:
container.append('<div style="margin-left:0;margin-right:0;width:0;"></div>');
setTimeout(function(){
// needs placing in a timeout so that
// the CSS change will actually transition
// (CSS changes made right after inserting an
// element into the DOM won't get transitioned)
container.children().last().css({
'margin-left': '',
'margin-right': '',
width: ''
});
},0);
There are 'jumps' in position when adding/removing elements because the flexbox is set with justify-content: space-around;
, so adding/removing an element (even one with zero width) will cause the 'space' between elements to be redistributed. I think it'd be fairly tricky to work around this.
How to animate inserts and removes in a container with justify-content?
The main problem is that the behavior you are getting is the expected one.
In the very same instant that card.remove()
is executed the flexbox justify-content
property need to adjust the gaps around the removed element (as you said). And, as Paulie D has pointed out, there is nothing to animate about.
The only solution I can think about is to skip the flex thing and use javascript to create the necessary gaps among the card elements.
Here I leave the snippet:
var animation_time = 500;
var column_height = $('.column').height();
var cards_height = $('.card').height();
var cards_number;
var cards_total_height;
var space_to_be_distributed;
var placeholder_height;
function updateSizes(cards_number)
{
cards_total_height = cards_number * cards_height;
space_to_be_distributed = column_height - cards_total_height;
placeholder_height = space_to_be_distributed / (cards_number + 1);
}
updateSizes($('.card').length);
$('.placeholder').height(placeholder_height);
$(document).on('click', '.card', function () {
var card = $(this);
card.animate({height: 0, opacity: 0}, animation_time, function () {
card.remove();
});
updateSizes($('.card').length - 1);
var placeholder = card.next();
$('.placeholder').not(placeholder).animate({height: placeholder_height}, animation_time);
placeholder.animate({height: 0}, animation_time, function () {
placeholder.remove();
updateSizes($('.card').length);
$('.placeholder').animate({height: placeholder_height}, animation_time);
});
});
$('a').click(function () {
var card = $('<div class="card">');
card.css({opacity: 0, height: 0})
.appendTo('.column')
.animate({height: 25, opacity: 1}, animation_time);
var placeholder = $('<div class="placeholder">');
placeholder.css({height: 0})
.appendTo('.column');
updateSizes($('.card').length);
$('.placeholder').animate({height: placeholder_height}, animation_time);
});
body, html, .column {
height: 100%;
margin: 0;
}
.column {
float: left;
width: 100px;
background: navy;
overflow: hidden;
}
.card {
height: 25px;
width: 100px;
background: grey;
}
.placeholder {
height: 25px;
width: 100px;
}
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
</head>
<body>
<div class="column">
<div class="placeholder"></div>
<div class="card"></div>
<div class="placeholder"></div>
<div class="card"></div>
<div class="placeholder"></div>
<div class="card"></div>
<div class="placeholder"></div>
</div>
<a href="#">Add card</a>
</body>
</html>
Related Topics
Scale HTML5 Video and Break Aspect Ratio to Fill Whole Site
<Div> Blur of Part of Background Image with CSS
Html/CSS Popup Div on Text Click
Adjacent Divs with Angled Borders
<Main> Element Not Working in Internet Explorer 11
How to Send an Inner <Div> to the Bottom of Its Parent <Div>
Difference Between Justify-Content VS Align-Items
Switching to Em-Based Media Queries
Responsive Iframe (Google Maps) and Weird Resizing
Bootstrap.Css: .Container:Before Display Table
Responsive Design with Media Query:Screen Size
IE7 CSS Inheritance Does Not Work
Css, Background-Repeat Distance
Adding a Small Picture on the Right Side of Textfield with CSS
Font Sizes: Ems VS Pixels... in 2011, Which One Should Be Used
Attribute Selector Where Value Equals Either a or B
Increase Size of List-Style-Bullet Type
How to Style an Anchor Tag to Look Like a Button with the Same Height as the Button