469,268 Members | 1,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Screen coordinates of object

1
Hi everybody!

I need a script, which calculates screen coordinates of given object (e.g. textbox or div). By "screen coordinates" I mean offset from upper left corner of the screen (not frame or browser window).

Thanks in advance.
Oct 4 '06 #1
11 5100
acoder
16,027 Expert Mod 8TB
Use the screenX/screenY properties.
May 20 '08 #2
hsriat
1,654 Expert 1GB
You won't be able to get that if the HTML is rendered itself. But if you have set the position, and left and top of the elements by CSS (or javascript), only then you would be able to retrieve the x and y coordinates as
Expand|Select|Wrap|Line Numbers
  1. var x = parseInt(elementObj.style.left); 
  2. var y = parseInt(elementObj.style.top);
May 21 '08 #3
hsriat
1,654 Expert 1GB
Use the screenX/screenY properties.
And that was a really old post! ...:D
May 21 '08 #4
Markus
6,050 Expert 4TB
And that was a really old post! ...:D
Must've been bored.

Like me
May 21 '08 #5
acoder
16,027 Expert Mod 8TB
You won't be able to get that if the HTML is rendered itself. But if you have set the position, and left and top of the elements by CSS (or javascript), only then you would be able to retrieve the x and y coordinates as
Expand|Select|Wrap|Line Numbers
  1. var x = parseInt(elementObj.style.left); 
  2. var y = parseInt(elementObj.style.top);
That would give you the x/y coordinates within the browser, not the screen

...and yes, it's an old post, but no-one answered it (poor thing!)
May 21 '08 #6
hsriat
1,654 Expert 1GB
Must've been bored.

Like me
But you don't reply in your own threads... :-/
May 21 '08 #7
hsriat
1,654 Expert 1GB
That would give you the x/y coordinates within the browser, not the screen
oh.. I see... good point..
So one can still calculate the browser's offset wrt the screen by doing
Expand|Select|Wrap|Line Numbers
  1. var offsetX = event.screenX - event.pageX; //(or clientX)
  2. var offsetY = event.screenY - event.pageY; //(or clientY)
And then add that in x and y calculated above...
what say? :p

...and yes, it's an old post, but no-one answered it (poor thing!)
I hadn't joined by the time. :p
May 21 '08 #8
acoder
16,027 Expert Mod 8TB
oh.. I see... good point..
So one can still calculate the browser's offset wrt the screen by doing
Expand|Select|Wrap|Line Numbers
  1. var offsetX = event.screenX - event.pageX; //(or clientX)
  2. var offsetY = event.screenY - event.pageY; //(or clientY)
And then add that in x and y calculated above...
what say? :p
screenX and screenY should be enough, see link.
I hadn't joined by the time. :p
Neither had I ;)
May 21 '08 #9
hsriat
1,654 Expert 1GB
screenX and screenY should be enough, see link.
But still, how would you tell what are the coordinates of a <div> element (for instance) wrt the screen? I'm not getting what you are trying to say.

This is what I understand from screenX and pageX:

May 21 '08 #10
acoder
16,027 Expert Mod 8TB
But still, how would you tell what are the coordinates of a <div> element (for instance) wrt the screen? I'm not getting what you are trying to say.
I see now what you mean. Yes, screenX/Y would give the screen co-ordinates, pageX/Y the co-ordinates relative to the document. However, it's not supported in IE, so you'll have to use clientX/Y plus scrolling offset in IE to get the same value.
May 21 '08 #11
hsriat
1,654 Expert 1GB
I see now what you mean. Yes, screenX/Y would give the screen co-ordinates, pageX/Y the co-ordinates relative to the document. However, it's not supported in IE, so you'll have to use clientX/Y plus scrolling offset in IE to get the same value.

I know its not an IE thing. But while having a logical conversation, we should keep IE out. :D
Of course it would need some fix as usual. :)
May 21 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by phil cunningham | last post: by
2 posts views Thread by Dan Sikorsky | last post: by
3 posts views Thread by ZorpiedoMan | last post: by
1 post views Thread by zoneal | last post: by
3 posts views Thread by Peter Oliphant | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.