how to animate element in table cell by cell?

2019-08-12 14:35发布

问题:

I have an an element in table cell and I have to move it in a direction to another cell with slow animation. How can i do it.

<table>
<td ..

</table>

look at the demo DEMO

I got some resources from the previous thread

I would like to move the element slowly cell by cell in the table? Is there anything missing in the demo?

回答1:

The code in your fiddle works as it should, but since you run the call of the animation in a loop, it is going to fast, you call all animation more or less simultaneously. I changed the method a bit:

$("#move").bind("click",animate);

var direction=[4,7,8,11]
function animate(){
    // initalize with first element of direction array
    moveAnimate("#p11",0);
}


function moveAnimate(element, count){
    if(count >= direction.length) {return; }
        // set the newParent
        var newParent = '#pos' + direction[count],
            element = $(element); //Allow passing in either a JQuery object or selector

    newParent= $(newParent); //Allow passing in either a JQuery object or selector
    var oldOffset = element.offset();
    element.appendTo(newParent);
    var newOffset = element.offset();

    var temp = element.clone().appendTo('body');
    temp.css('position', 'absolute')
        .css('left', oldOffset.left)
        .css('top', oldOffset.top)
        .css('zIndex', 1000);
    element.hide();
    temp.animate( {'top': newOffset.top, 'left':newOffset.left}, 'slow', function(){
        element.show();
        temp.remove();
        // increase the counter
        count++;
        // call next animation after the current one is finished
        moveAnimate(element,count);
    });
}

The updated fiddle is here.