开发者

determine mouse location on an image

开发者 https://www.devze.com 2023-01-20 00:39 出处:网络
how do i determine the mouse location on an image us开发者_JS百科ing javascript when mouse is clickedtrimmed version of the solution linked by geeketteSpeaks:

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

精彩评论

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