2015-09-29 184 views
2

我想拖放一個基於鼠標座標的元素。它在Chrome和IE中工作正常,但不能在Firefox中工作。在firefox中獲取鼠標座標41

下面是我曾嘗試:

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 

}

我怎麼拖高端鼠標的座標?

回答

2

不幸的是,Firefox並沒有在drag事件中填寫pageXpageY屬性。相關的bug report自2009年開始營業,看起來並不像近期那樣得到修復。常見的解決方案是父元素上的listen for dragover事件(儘管它使檢測哪個元素被拖動變得困難)或檢測被拖動元素的位置in some other way

+0

我認爲您在鏈接中提供的解決方案在最新的Firefox中不起作用 – Satyadev