如何设置可拖动对象相对于可放置对象的位置

时间:2013-07-14 04:28:20

标签: jquery drag-and-drop

嗨,我有一个名为bodyArea的div,我想将一个ibject拖入div ..

当我拖动对象时,我希望对象被附加到div处于完全相同的位置,在那里我将重新定位可拖动对象..

但是由于一些问题(我发现它无法正常工作......

这是我的代码

 $("#bodyArea").droppable({ 
                drop: function(event, ui){
                    //this is so that the element "sticks" even when tab is changed.
                    ui.draggable.addClass("draggedout");
                                    $(this).append(ui.draggable.css({'top': event.pageY-$(this).offset().top,'left': event.pageX-$(this).offset().left}));// this line is what i added extra.
//and its for this line that draggable object is getting appended but not at the desired position

                },
                //changes current tab to the tab the piece belongs to when dragged out of body area
                out: function(event, ui){
                    ui.draggable.removeClass("draggedout");
                    var whichTab = ui.draggable.parent().attr("id");
                    $("#piecesArea").tabs('select' , whichTab);
                }
            });

1 个答案:

答案 0 :(得分:0)

我不熟悉draggable / droppable jQuery UI功能,但您应该能够使用document.elementFromPoint( x, y )在drop事件下获取该元素,并根据需要在该元素之前或之后插入您的对象。

$this.hide(); // hide it momentarily so the unlerlying element is visible
element = document.elementFromPoint( event.pageX, event.pageY );
$this.insertBefore(element).show();
相关问题