I have a generic HTML like this
<div>
<span>Item 1</span>
<span>Item 2</span>
<span>Item 3</span>
<span>Item 4</span>
</div>
And I need to implement a drag-to-change-order, being able to drag each item to change the order, like you can drag an image in a text editor and drop it between words, similar to how you you can do in Mail on ios or in the Mac Dock, but with text.
Like this: http://img5.imageshack.us/img5/1921/v57sibdzorpefuaqozisso.mp4
When a user drag something and change the order, I need the actual DOM to be in that order, and not just appear on the screen like when you do with position:absolute
.
The first idea I had is to lift the element to position absolute, computing the coordinates, and when you drop, calculate where it should be relative to other elements and then position:relative
back.
But I'm not sure if it's a good approach, and the implementation is not quite simple. And it's not what I would consider a elegant solution, and I'm concerned about it turning jerky.
I rather like vanilla javascript then jquery, but it can help me to implement my own.
I already implemented drag a and drop in javascript but for absolute positioned elements and it doesn't work on relative positioned elements.
I'm looking forward ideas.