Reputation: 77
I use jquery to alter a table's td contents. I want to "move" the content of one td to another td, replacing the existing content.
E.g.:
| A | B |
| C | D |
now i "move the content of D to A" and get:
| D | B |
| C | |
how can I animate this transition? Fade-out/fade-in is easy but how can one visually move D to A?
Thanks very much
Update: I need to do it programatically, so user drag/drop is not an option.
Upvotes: 2
Views: 2342
Reputation: 108500
Here is one way:
DIV
and append the contents of the TD
TD
DSomething like this: (demo: http://jsfiddle.net/fvLr6z5s/)
var $d = $('.tdd')
var $a = $('.tda')
var offsetD = $d.offset()
var offsetA = $a.offset()
var $clone = $('<div>')
.addClass('clone')
.append($d.contents())
.css(offsetD)
.appendTo('body')
$clone.animate(offsetA, function() {
$a.empty().append($clone.contents())
$clone.remove()
})
Upvotes: 1
Reputation: 1067
you should move the <td>
tags along.here I made you a jsfiddle
and here are my codes:
<table>
<tr>
<td id="moveto">A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td id="movefrom">D</td>
</tr>
</table>
<input type='button' id='change'value='change'>
Js
$(function() {
$('#change').click(function(){
$( "#movefrom" ).animate({
left: "-=16", //you should change this numbers based on your table size
top: "-=22",
}, 5000, function() {
// Animation complete.
});
});
});
Upvotes: 0
Reputation: 2556
You need wrapp your content in td in <div>
, and get A and D cells absolut positions $(element).offset();
, and with .animate()
move it. After that you need remove hide div, and moved div replace in new cell.
$(function(){
var posF = $('#first').offset();
var posS = $('#second').offset();
$('#second').css({
position: 'absolute',
top: posS.top,
left: posS.left
});
$('#first').fadeOut(function(){
$(this).remove();
});
$('#second').animate({
top: posF.top,
left: posF.left
}, 1000);
});
Upvotes: 1