I'm using jQuery and Sortable to arrange my list of items (and this http://dragsort.codeplex.com).
All works perfect.
I'm using a function on dragEnd
to arrange the lists in order.
Here is my code:
$("#list1, #list2").dragsort({ dragSelector: "div",
dragBetween: true,
dragEnd: saveOrder,
placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });
function saveOrder() {
var data = $("#list1 li").map(function() { return $(this).children().html(); }).get();
$("input[name=list1SortOrder]").val(data.join("|"));
};
My question: Is there anyway that I can do an animation while I'm dragging? Or reposition the elements while dragging? I just need it to work on Safari.
One example is this:
http://www.youtube.com/watch?v=U3j7mM_JBNw
Look at the drag/drop (0:30) and you'll see what I'm talking about.
Thanks.
A bit late to the party, but I was determined to get a solution going with jQuery as there was very little help on this topic, especially replicating the functionality that exists on web apps like Facebook and their albums' images dragging and dropping to reorder, and the pretty animations that go along with that...
So I've come up with a solution that seems to work pretty great, and I'll do my best to explain it to the best of my abilities! Here goes...
The biggest problem here was to not only animate the sortables, but to figure out where they needed to animate to - fantastic when it comes to floating elements like images in a gallery! To get around this, I decided to
.clone()
the original floatingLI
items, position the clones absolutely under the originalLI
items using az-index
value that was less than the originalLI
items, and then when thechange
event fired from the jQuery sortable I could detect the position of the originalLI
and animate the absolutely positioned clones to those positions. The rest was to simply show / hide elements appropriately to get the desired effect.Here's the code, starting with the HTML:
So we have the original items we're trying to sort, and a container for the cloned items. Time for the CSS:
With our CSS, we're just removing any list styling, floating our original elements, and setting up the
z-index
requirements to ensure the cloned items lie underneath the original items. Note therelative
position on the original items to make sure they behave as expected. Why underneath you ask? It will (hopefully) become clear with some Javascript:Wow, I really hope this makes sense and helps someone animate their sortable lists, but this is a working example for anyone who's interested! :)
While this solution works great to create an initial transition, when the item snaps back, there is no transition. The solution is easier than I ever expected. All you need to do is adjust the revert option in .sortable()
Like this:
jQuery UI API: http://api.jqueryui.com/sortable/#option-revert
This makes a nice and smooth transition to the item's new home.
Click here for EXAMPLE on jsFiddle
Just impleneted what Chris Kempen said: http://jsfiddle.net/dNfsJ/
From the jsfiddle answer above (http://jsfiddle.net/KgNCD/2/):
Why did not you used Sortable on jqueryui? http://jsfiddle.net/KgNCD/
JS:
HTML: