Firefox 沒有 offsetX 和 offsetY,解決方法是從父層把left 和top做累加。
function getOffset(e) {
/*e: Mousemove Event*/
if (e.target.offsetLeft == undefined) {
/*firefox only*/
e.target = target.parentNode;
}
var tarPos = getTarPos(e.target);
var tarMousePos = { x: window.pageXOffset + e.clientX, y: window.pageYOffset + e.clientY };
var offset = { offsetX: tarMousePos.x - tarPos.x, offsetY: tarMousePos.y - tarPos.y };
return offset;
}
function getTarPos(el) {
var page = { x: 0, y: 0 };
while (el) {
page.x += el.offsetLeft;
page.y += el.offsetTop;
el = el.offsetParent;
}
return page;
}