Jquery-ui保存可拖动项目的位置

时间:2022-12-06 15:44:29

I want to keep the var of the x and y position of the draggable item on stop. Thank to some fiddle and topic i did this :

我想保持可拖动项目的x和y位置的var停止。感谢一些小提琴和主题,我做了这个:

$("#image").draggable({
        helper: 'clone',
    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());
    }
});

I just want to save the position each time I move the item and use it in an other javascript function.

我只想在每次移动项目时保存位置并在其他javascript函数中使用它。

Here is the fiddle.js :

这是fiddle.js:

http://jsfiddle.net/oe0fg84b/

2 个解决方案

#1


0  

As mentioned by @John, it looks like you're already storing the x and y coordinates of the draggable image in elements #source_x and #source_y, respectively.

正如@John所提到的,看起来你已经将可拖动图像的x和y坐标分别存储在元素#source_x和#source_y中。

You should be able to access them in another function as follows:

您应该能够在另一个函数中访问它们,如下所示:

var x =$("#source_x").val(); 
var y = $("#source_y").val();

Just an observation, your helper property clone seems to be causing the image to reset it's position after being dragged. I suspect this is then overriding your coordinate values, resetting to 0,0 each time. Try:

只是一个观察,你的助手属性克隆似乎导致图像在被拖动后重置它的位置。我怀疑这会覆盖你的坐标值,每次重置为0,0。尝试:

$("#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());
    }
});

I've modified your example to later get the coordinates upon button click: http://jsfiddle.net/Jason_Graham/oe0fg84b/2/

我修改了你的例子以便稍后点击按钮获得坐标:http://jsfiddle.net/Jason_Graham/oe0fg84b/2/

#2


0  

Try using the below code. If I got you correct, probably this can be the solution.

尝试使用以下代码。如果我告诉你的话,这可能就是解决方案。

var moved_times_index = 0;
var postionssaved_object = {};
$("#image").draggable({
        helper: 'clone',
    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());
        moved_times_index++;
        postionssaved_object[moved_times_index] =   [$("#source_x").val(), $("#source_y").val()];
    }
});

#1


0  

As mentioned by @John, it looks like you're already storing the x and y coordinates of the draggable image in elements #source_x and #source_y, respectively.

正如@John所提到的,看起来你已经将可拖动图像的x和y坐标分别存储在元素#source_x和#source_y中。

You should be able to access them in another function as follows:

您应该能够在另一个函数中访问它们,如下所示:

var x =$("#source_x").val(); 
var y = $("#source_y").val();

Just an observation, your helper property clone seems to be causing the image to reset it's position after being dragged. I suspect this is then overriding your coordinate values, resetting to 0,0 each time. Try:

只是一个观察,你的助手属性克隆似乎导致图像在被拖动后重置它的位置。我怀疑这会覆盖你的坐标值,每次重置为0,0。尝试:

$("#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());
    }
});

I've modified your example to later get the coordinates upon button click: http://jsfiddle.net/Jason_Graham/oe0fg84b/2/

我修改了你的例子以便稍后点击按钮获得坐标:http://jsfiddle.net/Jason_Graham/oe0fg84b/2/

#2


0  

Try using the below code. If I got you correct, probably this can be the solution.

尝试使用以下代码。如果我告诉你的话,这可能就是解决方案。

var moved_times_index = 0;
var postionssaved_object = {};
$("#image").draggable({
        helper: 'clone',
    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());
        moved_times_index++;
        postionssaved_object[moved_times_index] =   [$("#source_x").val(), $("#source_y").val()];
    }
});