event.offsetX in Firefox

后端 未结 11 1816
既然无缘
既然无缘 2020-11-27 16:45





        
11条回答
  •  無奈伤痛
    2020-11-27 17:07

    Unfortunately offsetX and layerX are not exactly the same as offsetX is the offset within the current element but layerX is the offset from the page. Below is a fix I am currently using for this:

    function fixEvent(e) {
        if (! e.hasOwnProperty('offsetX')) {
            var curleft = curtop = 0;
            if (e.offsetParent) {
               var obj=e;
               do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
               } while (obj = obj.offsetParent);
            }
            e.offsetX=e.layerX-curleft;
            e.offsetY=e.layerY-curtop;
        }
        return e;
    }
    

提交回复
热议问题