Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-28-2009, 05:55 PM   PM User | #1
blabus
New to the CF scene

 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
blabus is an unknown quantity at this point
How Do I Get the Absolute Position of a DIV?

Before you immediately give me an answer, let me describe fully what I'm trying to do, as it's not as simple as the title suggests.

I need to get the absolute position of a DIV (top and left properties) relative to the viewport/screen.

I've tried using .offsetTop and .offsetLeft, but these return the top and left properties relative to the DIV's immediate parent element, not the viewport (unless of course the DIV is top-level and not contained in any other DIVs).

So, is there any way to retrieve the absolute position of a DIV relative to the viewport, even if that DIV is nested in other DIVs?

Thanks for any help!
blabus is offline   Reply With Quote
Old 06-28-2009, 06:58 PM   PM User | #2
scrappy
New Coder

 
Join Date: Nov 2008
Posts: 59
Thanks: 5
Thanked 4 Times in 4 Posts
scrappy is an unknown quantity at this point
Go through all the parents adding up all the positions, like so:

Code:
var el=document.getElementById('id');
var cl=0,ct=0;
if (el.offsetParent)
{
	do
	{
		cl+=el.offsetLeft;
		ct+=el.offsetTop;
	}
	while(el=el.offsetParent);
}

Last edited by scrappy; 06-28-2009 at 07:00 PM..
scrappy is offline   Reply With Quote
Reply

Bookmarks

Tags
div, left, location, position, top

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:36 AM.


Advertisement
Log in to turn off these ads.