0

当有人在我的虚拟游戏中点击我的字段时,我会得到我当前的左侧和顶部坐标:

 function moveDiv(fromSeat, toSeat){
    var fromTop  = $('#seat-' + fromSeat).css("top");
    var fromLeft = $('#seat-' + fromSeat).css("left");

    var toTop  = $('#seat-' + toSeat).css("top");
    var toLeft = $('#seat-' + toSeat).css("left");

    /** Move a div from point {"fromTop", "fromLeft"} => {"toTop", "topLeft"} **/
  }

我被困在这里,我试图制作一个 $('...').animate({}); 但无法让它工作。我希望 div "#test" 从点 {"fromTop", "fromLeft"} => {"toTop", "topLeft"} 移动

4

1 回答 1

0

您可能需要添加顶部值并添加左侧值并使用总计进行动画处理

function moveDiv(fromSeat, toSeat){
    var fromTop  = $('#seat-' + fromSeat).css("top");
    var fromLeft = $('#seat-' + fromSeat).css("left");

    var toTop  = $('#seat-' + toSeat).css("top");
    var toLeft = $('#seat-' + toSeat).css("left");

     var position={ top: fromTop+ toTop, left: fromLeft+toLeft}

     $('#seat-' + fromSeat).animate( position);

  }

编辑:需要将值从包含的字符串转换px为数字以添加它们。可以parseInt()用来做

于 2013-01-13T22:42:51.910 回答