Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

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

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 warning on webkit, I've done that :

var x = evt.offsetX || evt.layerX,
    y = evt.offsetY || evt.layerY;

But this seem quite complex ! Is that really what we should do to get layerX working in all browsers ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...