在firefox 41

时间:2015-09-29 08:25:29

标签: javascript jquery firefox drag-and-drop mouse-coordinates

我正在尝试根据鼠标坐标拖放元素。它在chrome和IE中运行良好,但在firefox中无法正常工作。

以下是我的尝试:

function createShape(event)
{
    var stageContainer = $(".mainArea");
    var stageOffset = stageContainer.offset();
    clientX = event.clientX;
    clientY = event.clientY;
    offsetX = stageOffset.left;
    offsetY = stageOffset.top;
    x = clientX - offsetX; // works in chrome and IE
    y = clientY - offsetY; // works in chrome and IE

    //Firefox
    var offX = (event.offsetX || event.pageX - $(event.target).offset().left);
    var offY = (event.offsetY || event.pageY - $(event.target).offset().top);
    layerX = event.layerX;
    layerY = event.layerX;
    screenX = event.screenX;
    screenY = event.screenY;
    pageX = event.pageX;
    pageY = event.pageY;
    e = jQuery.event.fix(event);
    var target = event.target || event.srcElement;

    //Right now this is working but x and y are not exactly correct
    x = screenX + offsetX
    y = screenY + offsetY

在Firefox中不起作用

ClientXY return 0,0
LayerXY return largest number like 173839,173839
pageX and pageY returns 0,0
element.offset().left returns 70 always
element.offset().top returns 63 always

createRect(x, y); // creating element on dragend with x and y coordinates on dragend

}

如何获得拖动结束鼠标坐标?

1 个答案:

答案 0 :(得分:3)

可悲的是,Firefox并未在pageX个事件中填充pageYdrag个属性。相关的bug report自2009年开放,看起来不会很快得到修复。常见的解决方案是在父元素上发生listen for dragover事件(尽管它很难检测被拖动的元素)或检测被拖动元素的位置in some other way