I'm using CSS transitions with the transform
property to shrinks elements when adding and removing them.
However one problem with this is that this property doesn't affect the flow of other elements, so it appears as though the element being deleted shrinks away, and then the rest of the elements jump suddenly.
If I were to animate the height property instead of using a transform this would be fine, however in actual usage I am using elements of variable height so I won't know what heights I can animate between.
Edit: people have suggested animating the height
property (which won't work as stated above), or the max-height
property. The max-height
property will work to some extent, however you cannot align the timings perfectly as the transition will keep adjusting the max-height
property past the actual height of the element until the end of the transition time.
Another problem with these approaches is that it does not use the smooth animations that you can achieve with the transform
property. The object's transform will happen smoothly, however the movement of the following elements will stutter as the browser renders these transitions differently.
Here's a JSFiddle with what I mean (try adding then removing elements, and see the jump when elements are removed):
var button = document.querySelector("button");
var box = document.createElement("div");
box.className = "box";
box.appendChild(document.createTextNode("Click to delete"));
button.addEventListener("click", function(e) {
var new_box = box.cloneNode(true);
new_box.addEventListener("click", function(e) {
this.className = "box deleting";
window.setTimeout(function(e) {
new_box.remove();
}, 1000);
});
this.parentNode.appendChild(new_box);
});
button {
font-size: 20pt;
}
.box {
font-size: 20pt;
margin: 10px;
width: 200px;
padding: 10px;
background: pink;
transform: scale(1, 1);
transform-origin: top left;
}
.deleting {
transform: scale(1, 0);
transition: all 1000ms ease;
}
<button>
Add Box
</button>