Jquery draggable position - clone

拜拜、爱过 提交于 2019-12-06 16:16:24

问题


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?


回答1:


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); 



回答2:


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



来源:https://stackoverflow.com/questions/5949172/jquery-draggable-position-clone

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