Use the function getCoords() from the chapter Coordinates to get document-relative coordinates.
jQuery . offset() will get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.
If the element is in the main document you can get the DIV's coordinates with... var X=window. getComputedStyle(abc,null). getPropertyValue('left'); var Y=window.
To get the X and Y coordinates for a div element with JavaScript, we an use the getBoundingXClientRect method. to select the div with querySelector . And then we call getBoundingClientRect to get the position of the div. Then we get the x and y coordinates of the div with position.
window.screenX/Y
are not supported on IE. But for other browsers, a close approximation of position is:
var top = $("#myelement").offset().top + window.screenY;
var left = $("#myelement").offset().left + window.screenX;
Exact position depends on what toolbars are visible. You can use the outer/innerWidth
and outer/innerHeight
properties of the window
object to approximate a little closer.
IE doesn't provide much in the way of window properties, but oddly enough, a click event object will provide you with the screen location of the click. So I suppose you could have a calibration page which asks the user to "click the red dot" and handle the event with
function calibrate(event){
var top = event.screenY;
var left = event.screenX;
}
Or possibly use the mouseenter/leave
events using the coordinates of that event to calibrate. Though you'd have trouble determining if the mouse entered from the left, right, top, or bottom.
Of course, as soon as they move the screen you'll need to recalibrate.
For lots more on browser property compatibilities see PPK's Object Model tables.
I disagree with jvenema's answer.
I have found a solution that is working for me on google chrome anyway, but I hope that this will works on many other browsers too (maybe not on some old internet explorers).
Inside the <script>
tags declare new two global variables called: pageX and pageY with the var
keyword.
These global variables should always store the coordinates of the top left corner of the rendered page relative to the top left corner of the screen monitor, but in order to achieve this goal, the window.onmousemove
should refers to a function that sets the pageX to the difference of event.screenX and event.clientX, and the pageY to the difference of event.screenY and event.clientY.
Then all what you need to do is to get the coordinates of an element (relative to the top left corner of the rendered page) and add pageX and pageY. The results of these expressions will be the coordinates of this element relative to the top left corner of the screen monitor!
Code example (only javascript):
<script>
var pageX;
var pageY;
window.onmousemove = function (e) {
pageX = e.screenX - e.clientX;
pageY = e.screenY - e.clientY;
}
function getScreenCoordinatesOf(obj) {
var p = {};
p.x = pageX + obj.clientLeft;
p.y = pageY + obj.clientTop;
return p;
}
</script>
Hope that helps!
I don't think this is possible.
The element coordinates are relative to the top left corner of the rendered page.
The window coordinates are relative to the screen.
There is, to my knowledge, nothing that relates the top left corner of the rendered page to the top left corner of the window. So there's no way to account for borders, scrollbars, chrome, etc. So I think you're sunk on this one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With