开发者

determine mouse location on an image

how do i determine the mouse location on an image us开发者_JS百科ing javascript when mouse is clicked


trimmed version of the solution linked by geeketteSpeaks:

function getImageCoords(event, img) {
  var posX = event.offsetX ? (event.offsetX) : event.pageX - img.offsetLeft;
  var posY = event.offsetY ? (event.offsetY) : event.pageY - img.offsetTop;
  alert("You clicked at: (" + posX + "," + posY + ")");
}
<img src="https://via.placeholder.com/350x150" onclick="getImageCoords(event,this);">


Is this what you are looking for?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜