Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

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 04-22-2009, 01:33 AM   PM User | #1
ma.mazmaz
New to the CF scene

 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ma.mazmaz is an unknown quantity at this point
Absolute Position of element inside scrollable div

I have an element inside a <div> with overflow:auto

I have been trying to find the absolute position of that element. The following code is what I have been using and does not work...

Code:
function ElemAbsPos(obj){
	var x=0;
	var y=0;
	
	while(obj){
		y+=obj.offsetTop;
		y-=obj.scrollTop;
		x+=obj.offsetLeft;
		x-=obj.scrollLeft;
		obj=obj.offsetParent;
	}
	
	return [x,y];
}
ma.mazmaz is offline   Reply With Quote
Old 04-22-2009, 03:39 AM   PM User | #2
TinyScript
Regular Coder

 
Join Date: Mar 2009
Location: Portland Oregon
Posts: 690
Thanks: 44
Thanked 63 Times in 62 Posts
TinyScript is on a distinguished road
have you tried getComputedStyle
https://developer.mozilla.org/En/DOM...tComputedStyle
TinyScript is offline   Reply With Quote
Old 04-22-2009, 09:30 PM   PM User | #3
ma.mazmaz
New to the CF scene

 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ma.mazmaz is an unknown quantity at this point
Yes, I have just tried that. Since I am trying to find the position of the element as it is displayed globally (based on body element) on the page, that method does not work. It will give me the position as based upon its parent, without taking into account scrolling or any parent elements.
ma.mazmaz is offline   Reply With Quote
Old 04-22-2009, 11:25 PM   PM User | #4
TinyScript
Regular Coder

 
Join Date: Mar 2009
Location: Portland Oregon
Posts: 690
Thanks: 44
Thanked 63 Times in 62 Posts
TinyScript is on a distinguished road
Quote:
Originally Posted by ma.mazmaz View Post
Yes, I have just tried that. Since I am trying to find the position of the element as it is displayed globally (based on body element) on the page, that method does not work. It will give me the position as based upon its parent, without taking into account scrolling or any parent elements.
use that on the parent element too. I guess I must not understand your problem
TinyScript is offline   Reply With Quote
Old 04-24-2009, 11:49 PM   PM User | #5
randomuser773
Banned

 
Join Date: Nov 2008
Location: not found
Posts: 284
Thanks: 0
Thanked 53 Times in 51 Posts
randomuser773 can only hope to improve
Quote:
Originally Posted by ma.mazmaz View Post
Since I am trying to find the position of the element as it is displayed globally (based on body element) on the page,
If by that you mean its current displacement from the top of the body element, does it make sense to subtract the scrollTop/Left of the body element? This isn't tested but may be worth a try:
Code:
function ElemAbsPos(obj){
	var x=0;
	var y=0;
	
	while(obj){
		y += obj.offsetTop;
		y -= obj.offsetParent ? obj.scrollTop : 0;
		x += obj.offsetLeft;
		x -= obj.offsetParent ? obj.scrollLeft : 0;
		obj=obj.offsetParent;
	}
	
	return [x,y];
}
randomuser773 is offline   Reply With Quote
Reply

Bookmarks

Tags
css, dom, position

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:52 AM.


Advertisement
Log in to turn off these ads.