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/
精彩评论