开发者

Click location in image using JQuery

开发者 https://www.devze.com 2023-03-06 00:59 出处:网络
is there an easy way to find where user clicked inside an 开发者_如何学运维image (div, ...), relative to top-left corner of the element? (using js/jquery)

is there an easy way to find where user clicked inside an 开发者_如何学运维image (div, ...), relative to top-left corner of the element? (using js/jquery)

Basic event.pageX/event.pageY does not take into account scrolling and element position. Combining Document.getScrollTop() and element.getAbsoluteTop ( Mouse click location on an image ) does not look nice at all (may not even work on all browsers as far as I know).

Is there a simpler way to this?


This seems simple enough:

$('#yourImg').click(function(e){
    var x = e.pageX - e.target.offsetLeft,
        y = e.pageY - e.target.offsetTop;
});

See demo →

0

精彩评论

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

关注公众号