One approach would be to replace the div to be removed with a placeholder, then animate both the placeholder and the original.
Using this snippet: Positioning an element over another element with jQuery
Your code can be changed to something like this (I used click on the item to trigger the removal here):
Demo
CSS
div { box-sizing: border-box; }
.item, .placeholder {
float: left;
width: 100px;
height: 100px;
border: 1px solid black;
margin: 5px;
position: relative;
background: white;
-webkit-transition: all 1s ease;
}
.placeholder {
opacity: 0;
border: 0px;
}
.item.hide {
border: 1px solid rgba(0,0,0,0);
margin: 0px;
top: 500px;
opacity: 0;
overflow: hidden;
z-index: -1;
}
.placeholder.hide {
width: 0;
height: 0;
margin: 0;
border: 0;
}
Script
$('.item').on('click', function(evt) {
var $this = $(this);
var $new = $($this.clone());
$new.addClass('placeholder');
$this.replaceWith($new);
$this.positionOn($new);
$this.appendTo($('body'));
setTimeout(function() {
$this.css({top: '', left: ''});
$this.addClass('hide');
$new.addClass('hide');
}, 0);
});
Snippet from: Positioning an element over another element with jQuery
// Reference: http://snippets.aktagon.com/snippets/310-positioning-an-element-over-another-element-with-jquery
$.fn.positionOn = function(element, align) {
return this.each(function() {
var target = $(this);
var position = element.position();
var x = position.left;
var y = position.top;
if(align == 'right') {
x -= (target.outerWidth() - element.outerWidth());
} else if(align == 'center') {
x -= target.outerWidth() / 2 - element.outerWidth() / 2;
}
target.css({
position: 'absolute',
zIndex: 5000,
top: y,
left: x
});
});
};
Now your old div animates out of the way while the grid collapses.
If you are willing to add another library to your code, take a look at Masonry/Isotope by Metafizzy. They were designed to do just this sort of thing.