first things first. if we consider the css structure of elements. and with regards to floating left property your lis will obey the browser realignment algorithm.
so in order to take control of how they are going to be stacked you need to position them as absolute.
sorry but i am not going write code for this, this time in the night but will share an algorithmic thought.
then from this post you can learn how to detect a removed element. Detect removed elem
after that if something is removed detecting its index in the stack should be easy.
then you need a function (assuming all box sizes are equal) to update the left position (css left) of the each element unless its index determines that it is at the beginning or end of a queue in which case its css top position also requires updating.
the distinguishing of an element whether if it is existing on the beginning or end of the queue should be gone about using its index.
in a manner which if its containers width is for example 400 px when it should contain 4 boxes in each row. so if the new box is adding a width which makes the sum more than the boxes which already exist in that row more than the total sum then the first index in that row is at the beginning and the apposite accordingly for the last element in a queue.