はここで例http://jsfiddle.net/naqbq/jQueryからドラッグ可能な位置xとyを取得するにはどうすればよいですか?
はどのようにして再配置画像の後x
とy
の現在の位置をつかむのですか?
<input type="hidden" name="source_x" id="source_x" />
<input type="hidden" name="source_y" id="source_y" />
はここで例http://jsfiddle.net/naqbq/jQueryからドラッグ可能な位置xとyを取得するにはどうすればよいですか?
はどのようにして再配置画像の後x
とy
の現在の位置をつかむのですか?
<input type="hidden" name="source_x" id="source_x" />
<input type="hidden" name="source_y" id="source_y" />
、あなたはドラッグされた要素にアクセスするためにui.helper
を使用することができます。ブラッドが示唆したように続いて、その上に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の.offset()
を使用できます。 stop
コールバックで
alert($('#image').offset().top);
alert($('#image').offset().left);
あなたがトップを得ることができ
$("#image").draggable({
stop: function() {
// check for positioning here using .css or .offset
}
});
、およびLEF:イベント
$('selector').draggable({
drag: function(event, ui) {
ui.position.top; // .left
// or
$('selector').position().top; // current position of an element relative to the offset parent
$('selector').offset(); // retrieves the current position relative to the document.
}
})
ありがとう@mgibsonbr :) –