how to animate element in table cell by cell?

牧云@^-^@ 提交于 2020-01-05 07:42:37

问题


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.



来源:https://stackoverflow.com/questions/14848299/how-to-animate-element-in-table-cell-by-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!