I have the following code to animate a dynamic div up and down. The idea is that there could be any number of divs that need to be moved up and down the stack, swapping positions with the divs above and below. Once in their new position, I need to be able to catch that new re-odered position. The following code works, once, but once a lower div moves up and a upper moves down (swap spots) to the new positions, they stop working. How can I set this up so that they will continue to traverse up the stack, swapping the one immediately above or below as they go? I also need to know the new positions once finished to update the database. Ive been looking everywhere but can’t seem to find how to do this. Any help would be much appreciated.
$('.editUp', this).click(function() {
thisRowHt = $(this).parents('.rowCont').outerHeight();
upperRowHt = $(this).parents('.rowCont').prev().outerHeight();
$(this).parents('.rowCont').animate({'top': '-=' + thisRowHt + 'px'});
$(this).parents('.rowCont').prev().animate({'top': '+=' + upperRowHt + 'px'});
return false;
});
$('.editDown', this).click(function() {
thisRowHt = $(this).parents('.rowCont').outerHeight();
lowerRowHt = $(this).parents('.rowCont').next().outerHeight();
$(this).parents('.rowCont').animate({'top': '+=' + lowerRowHt + 'px'});
$(this).parents('.rowCont').next().animate({'top': '-=' + thisRowHt + 'px'});
return false;
});
You should swap the elements in the DOM also, because when you animate the HTML elements, they just change theirs place on the screen.
I have completed your script:
You can see it in action here: Animate div up and down