Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  188] [ 1]  / answers: 1 / hits: 32900  / 13 Years ago, fri, july 8, 2011, 12:00:00

I am trying to get the absolute position (top and left) of a mouse click relative to the browser/body, not any parent elements within the body.



I have a listener bound to the body, but e.pageX and e.pageY are giving me the position relative to a div.



Note that I can leverage jQuery and YUI functions.



Code that currently does not work correctly:



//getting the position
function _handleClick(e) {
var data = { absX: e.pageX, absY: e.pageY};
_logClickData(data);
}

//binding the function
var methods = {
init: function () {
$(body).click(_handleClick);
}
};

More From » onclick

 Answers
131

According to this (http://docs.jquery.com/Tutorials:Mouse_Position), those should give you absolute positions. offsetX/Y gives you the relative position.



Edit November 2013: the original Mouse Position link seems to be broken, but the documentation for pageX contains an example that utilizes jQuery pageX/Y. The page about the offset method also contains relevant examples.


[#91284] Thursday, July 7, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;