0

有谁知道,我有一个可拖动的项目,它应该捕捉到可放置的项目。我有这个工作,但我似乎无法弄清楚的是,我希望可拖动对象一旦放到可放置对象上,就可以对齐自身,使其刚好离开左侧。就像下图一样。

有谁知道用 jQuerys 可拖动/可放置对象来做到这一点的方法?

更新

这是我在删除对象时拥有的功能。

function handleCardDrop( event, ui ) {
  var slotNumber = $(this).data( 'number' );
  var cardNumber = ui.draggable.data( 'number' );

  // If the card was dropped to the correct slot,
  // change the card colour, position it directly
  // on top of the slot, and prevent it being dragged
  // again
  //alert("Slot number: "+slotNumber+"\n Card Number: "+cardNumber);

  //alert(cardNumber+":"+slotNumber);
  if(cardNumber==slotNumber)
  {
      correctCards++;
  }

    ui.draggable.addClass( 'correct' );
    ui.draggable.draggable( 'disable' );

    ui.draggable.addClass( 'correct' );
    ui.draggable.draggable( 'disable' );
    $(this).droppable( 'disable' );

    ui.draggable.css({'cursor':'default'});
    ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
    ui.draggable.draggable( 'option', 'revert', false );
}

在此处输入图像描述

4

1 回答 1

2

I figured out a way to achieve this. I was using the position() function when what I really needed was the offset() function. This a snippet of the code I used for the positioning. The draggable object is a little bit smaller height, so for the top I calculate the different in size and then divide it by 2 to center it within the object. Then I just set it to half the draggable being off of the droppable object.

    var off = $( this ).offset();
    off.top += (($( this ).height() - ui.draggable.height())/2);
    off.left -= ui.draggable.width()/2;

    ui.draggable.offset( off );
于 2013-01-23T15:53:04.337 回答