有了拖拽,我怎样才能获得元素行进的距离?

时间:2013-04-02 01:45:52

标签: javascript jquery

我试图提取元素自拾起以来所经历的距离。我尝试抓住传递给offset处理程序的position事件的targetstop,但它们总是为零...

1 个答案:

答案 0 :(得分:0)

您可以在drag回调中查看行进方向,如下所示:

var diffX = 0;
var diffY = 0;

$('#some_div').draggable({
    drag: function(e, ui) {
        var start = ui.originalPosition,
            end = ui.position;

        diffX = end.left - start.left;
        diffY = end.top - start.top;
    },
    stop: function(e, ui) {
        console.info('Dragged: X = %o, Y = %o', diffX, diffY);
    }
});