获取图像像素坐标相对左上角

Joh*_*lta 5 javascript

我需要在点击相对左上角获取图像坐标.所以图像的左上角是条件0,0然后第一个像素1,0 ... 2..0等.在javascript构造中有什么东西可以用来获得这个逻辑吗?

use*_*087 6

这是一个可以帮助你的链接

http://www.emanueleferonato.com/2006/09/02/click-image-and-get-coordinates-with-javascript/

  • 注意:这里的答案确实应该包含正确的答案,而不是指向它的链接,以防其他网站出现故障。 (2认同)

Rah*_*thi 5

你可以尝试这样的事情: -

  <img id="board" style="z-index: 0; left: 300;position: absolute; top: 600px" align=baseline     border=0 hspace=0 src="design/board.gif">

  function findPos(obj){
  var curleft = 0;
  var curtop = 0;

  if (obj.offsetParent) {
do {
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
   } while (obj = obj.offsetParent);

return {X:curleft,Y:curtop};
 }
}

 findPos(document.getElementById('board'));
 alert(curleft);
 alert(curtop);
Run Code Online (Sandbox Code Playgroud)