Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery insertBefore/insertAfter animation not working

Tags:

jquery

I made a simple script that allows users to sort divs using up and down buttons.

jsFiddle: http://jsfiddle.net/dZ6rC/5/

Unfortunately, my attempt to animate the move on mouse click does not cause the tags to be moved.

This is my code:

$('.up').click(function(){
  var previous = $(this).parent().parent().prev('.item');
  $(this).parent().parent().insertBefore(previous).slideUp();
});
$('.down').click(function(){
  var next = $(this).parent().parent().next('.item');
  $(this).parent().parent().insertAfter(next).slideDown();
});​

Can someone figure out why the tags don't move?

like image 731
dirk Avatar asked Dec 20 '12 02:12

dirk


2 Answers

You're not hiding the elements before sliding them down so the effect is not seen:

http://jsfiddle.net/dZ6rC/4/

You can just add .hide before .slideDown. I think you probably want to improve the animation a bit from there, but just keep this in mind.

like image 193
Explosion Pills Avatar answered Oct 04 '22 17:10

Explosion Pills


I am not completely sure what you are trying to accomplish but chaining 3 parent() calls is probably not the best way.

I am guessing this solves your problem:

$('.up').click(function(){
  $(this).parents('.item').slideUp();
});
$('.down').click(function(){
 $(this).parents('.item').slideDown();
});​

If I am missing a requirement please update your question.

like image 41
Trent Earl Avatar answered Oct 04 '22 17:10

Trent Earl