views:

3858

answers:

3

I have an image tag inside of a table cell, that I'd love to move to another table cell, and have that movement animated.

The code looks something like this...

<td id="cell1"><img src="arrow.png" alt="Arrow"/></td>
<td id="cell2"></td>

I'd like to move "arrow.png" to "cell2", and have some kind of transition effect, preferably with JQuery.

Any ideas?

Thanks!

+1  A: 

JQuery http://docs.jquery.com/Downloading_jQuery
JQuery Effects http://docs.jquery.com/Effects/animate#paramsoptions


Example

 $("#go1").click(function(){
      $("#block1").animate( { width:"90%" }, { queue:false, duration:3000 } )
         .animate( { fontSize:"24px" }, 1500 )
         .animate( { borderRightWidth:"15px" }, 1500);
    });
Ballsacian1
-1: this gives an example of animation, but not of rehoming.
DDaviesBrackett
+1  A: 

You'll need to do this in two steps: (1) animation (2) rehoming.

The animation you can take care of with .animate(), as @Ballsacian points out. The rehoming can be accomplished with .html() - for the example above,

var arrowMarkup = $('#cell1').html(); //grab the arrow
$('#cell1').html(""); //delete it from the first cell
$('#cell2').html(arrowMarkup); //add it to the second cell

Of course, you'll have to complicate that code to integrate the animation. And this way of doing it won't cause the selection (I'm assuming you're selecting a table row?) to activate rows between the old selection and the new one, as the arrow passes by them. That'd be even more complex to achieve.

DDaviesBrackett
+6  A: 

This is actually quite difficult because you have to remove and add it to the dom but keep it's position. I think your looking for something like this. Bassicly we don't animate either the arrow in #cell1 or #cell2. We just create a new one in the body-tag and animate that. That way we don't have to worry about the table cell positions because we can position relative to the document.

var $old = $('#cell1 img');
//First we copy the arrow to the new table cell and get the offset to the document
var $new = $old.clone().appendTo('#cell2');
var newOffset = $new.offset();
//Get the old position relative to document
var oldOffset = $old.offset();
//we also clone old to the document for the animation
var $temp = $old.clone().appendTo('body');
//hide new and old and move $temp to position
//also big z-index, make sure to edit this to something that works with the page
$temp
  .css('position', 'absolute')
  .css('left', oldOffset.left)
  .css('top', oldOffset.top)
  .css('zIndex', 1000);
$new.hide();
$old.hide();
//animate the $temp to the position of the new img
$temp.animate( {'top': newOffset.top, 'left':newOffset.left}, 'slow', function(){
   //callback function, we remove $old and $temp and show $new
   $new.show();
   $old.remove();
   $temp.remove();
});

I think this should point you in the right direction.

Pim Jager
you have a "great answer" badge from me :)
Sinan Y.
Haha, thanks! [15 chars]
Pim Jager