Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to imitate a sort event with animation using jQuery?

I've got a list of items, each containing a checkbox. When a checkbox is ticked I'd like that item to sort itself to the beginning of the list, preferably animating as if it were being dragged with the mouse (but without a mouse cursor).

Is this possible? I've had trouble triggering draggable/sortable events in the past. I'm open to alternate solutions, it just need to be obvious where the item has moved to.

Edit: I was originally trying to ask how to make this happen on a jQuery sortable() list, but I obviously didn't communicate that very well, so I've updated the question to match all the wonderful answers.

like image 381
peterjwest Avatar asked Aug 08 '11 15:08

peterjwest


2 Answers

Similar to avetarman's second example, this fiddle doesn't wait for the newly checked box to reach the top before the other ones take their rightful place. Instead, they all get animated together:

http://jsfiddle.net/ebiewener/Y5Mdt/1/

Ultimately, it's all about giving the row you're moving an absolute position so that it can slide up over the other rows, while giving the necessary CSS attributes to those other rows so that they don't immediately jump out of place when the selected row is removed from the document flow with the absolute positioning. Once that is done, the selected row loses it's absolute positioning, but is appended to the beginning of the rows. This allows all the css to essentially be "reset", so that you can proceed selecting new rows.

like image 179
maxedison Avatar answered Oct 30 '22 09:10

maxedison


Check this fiddle. No plugin is used here.

http://jsfiddle.net/bN8x6/

Check this update as well. It uses relative positioning. So, during animation the placeholder is being kept.

http://jsfiddle.net/bN8x6/1/

like image 38
avetarman Avatar answered Oct 30 '22 11:10

avetarman