开发者

Click location in image using 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜