What substitute should we use for layerX/layerY since they are deprecated in webkit?

前端 未结 3 1220
野趣味
野趣味 2020-12-16 11:15

In chrome canary, layerX and layerY are deprecated, but what should we use instead ?

I\'ve find offsetX but it doesn\'t work with Firefox. So to get layerX without w

3条回答
  •  感动是毒
    2020-12-16 11:37

    Here is a function to calculate layerX and layerY from a click event:

    function getOffset(evt) {
      var el = evt.target,
          x = 0,
          y = 0;
    
      while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
        x += el.offsetLeft - el.scrollLeft;
        y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
      }
    
      x = evt.clientX - x;
      y = evt.clientY - y;
    
      return { x: x, y: y };
    }
    

    Thanks a lot to Stu Cox for pointing out the two functions used to make this one.

提交回复
热议问题