I use CSS transitions with the transform property to compress elements when they are added or removed.
However, one of the problems is that this property does not affect the flow of other elements, so it seems that the deleted element is compressed, and then the remaining elements suddenly jump.
If I were to animate the height property instead of using a transform, that would be nice, however in real use I use variable height elements, so I wonβt know what heights I can animate between them.
Edit: people suggested animating the height property (which won't work as described above), or the max-height . The max-height will work to some extent, however, you cannot precisely align the timings, since the transition will continue to adjust the max-height for the actual height of the element until the end of the transition period.
Another problem with these approaches is that it does not use the smooth animations that you can achieve with the transform property. The transformation of the object will occur smoothly, but the movement of the following elements stutters, as the browser makes these transitions in different ways.
Here's a JSFiddle with what I mean (try adding, then deleting elements, and see the transition when deleting elements):
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>
source share