开发者

Jquery draggable position - clone

I have jquery draggable/droppable working with the containment and helper options set. What I'd like to do is to store the top and left parameters of the dropped item in two variables.

I have achieved this in the following example (drag the new document icon into the box) however the position reported back is the position relative to the original icon instead of the parent DIV. Both the icon and the droppable box are absolute positioned.

http://www.instructuk.com/drop.php

Does anyone know how to get the position relevant to the parent instead of 开发者_开发问答the icon?


You'll have to do the calculation yourself:

var
  draggableDocumentOffset = ui.helper.offset(),
  droppableDocumentOffset = $(this).offset(),
  left = draggableDocumentOffset.left - droppableDocumentOffset.left,
  top = draggableDocumentOffset.top - droppableDocumentOffset.top;

alert('Item was dropped at - Left: ' + left + ', Top: ' + top); 


It is maybe not the most beautiful solution but you can get the position of the drop in the document by:

x=event.pageX;
y=event.pageY;

And then use the coordinates of the droppble area to substract from x and y.

dx=$("#droparea").offset().left;    
dy=$("#droparea").offset().top;    
var left=x-dx;          
var top=y-dy;   

K

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜