Because you already use jQuery, my answer was quite easy to make
$(function(){
var move = function(){
var data = [0,0]
$('.items > li').each(function(){
var $this = $(this)
var height = $this.outerHeight(true)
var side = ($this.hasClass('left') ? 0 : 1)
$this.css('top', data[side])
data[side]+=height
})
}
$(window).on('resize', function(){
move()
})
$(document).on('click', '.items > li', function(){
$(this).toggleClass('left').toggleClass('right')
move()
})
move()
$('.items').removeClass('wait')
})
.items{
margin: 0;
padding: 0;
list-style: none;
}
.items > li{
display: table;
position: absolute;
padding: 10px;
color: #fff;
cursor: pointer;
-webkit-user-select: none;
user-select: none;
transition: .3s ease;
}
.items.wait > li{
visibility: hidden;
}
.items .left{
left: 0;
background-color: #1ABC9C;
}
.items .right{
left: 100%;
transform: translateX(-100%);
background-color: #E74C3C;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<ul class="items wait">
<li class="left">Item 1<br />With some additional text</li>
<li class="left">Item 2</li>
<li class="left">Item 3</li>
<li class="left">Item 4</li>
<li class="right">Item 5</li>
<li class="right">Item 6</li>
<li class="right">Item 7</li>
<li class="right">Item 8</li>
</ul>
The CSS makes sure that elements with class left
are on the left, and the ones with class right
are on the right, but because of the following two lines
left: 100%;
transform: translateX(-100%);
the left and transform value will be transitioned, but it will look as if right
is set to 0.
The script recalculates everything on 3 occasions
- document ready
- window resize
- when one of the items is being clicked
When you click one of the items, it will simply toggle it's class from left
to right
. After that, the recalculation is being done. It keeps a variable data
, which keeps track of how high each column has gotten with each item that's in it, and moves every one after that that much from the top.
This script can account for elements with margin, padding, multiple lines and images, if you want.
Also, the list has a class wait
, which hides all the elements until they're set for the first time. This prevents the user from seeing the items when they're not yet placed.
Hope this helps