这里是一个例子 http://jsfiddle.net/naqbq/
重新定位图像后如何获取x
和y
的当前位置?
<input type="hidden" name="source_x" id="source_x" />
<input type="hidden" name="source_y" id="source_y" />
最佳答案
在stop
回调中,您可以使用ui.helper
来访问拖动的元素。然后按照 Brad 的建议使用 offset
:
$("#image").draggable({
stop:function(event,ui) {
var wrapper = $("#wrapper").offset();
var borderLeft = parseInt($("#wrapper").css("border-left-width"),10);
var borderTop = parseInt($("#wrapper").css("border-top-width"),10);
var pos = ui.helper.offset();
$("#source_x").val(pos.left - wrapper.left - borderLeft);
$("#source_y").val(pos.top - wrapper.top - borderTop);
alert($("#source_x").val() + "," + $("#source_y").val());
}
});
然后,只需将其调整为您的包装器 - 减去其偏移量和边框宽度 - 并将其设置为您的输入的 val
即可。 (抱歉对边框进行了硬编码,但我无法使用 (编辑:就是这样!在 another question 中找到了解决方案)css
提取它)
关于jquery - 如何从 jQuery ui Draggable 获取位置 x 和 y?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9810929/