[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1657
  • Last Modified:

offsetParent and offsetTop Question

Hi,
I am trying to understand how these two functions work.
Lets say I have a Button nested into two Tables I want to get the Position of that Button.

What I do is the following so far

alert(event.srcElement.offsetTop)

it works fine if there is no nesting, but if it is nested then the Result is not Accurate.

I tried to do something like this..
alert(event.srcElement.offsetParent.offsetTop+event.srcElement.offsetTop)

but still its not correct.

so I want not just to DO this project, I want to UNDERSTAND the logic how should i solve the issue.

Please Explain.(Exact Code is not required)

Thanks
0
khansoul
Asked:
khansoul
  • 3
  • 2
  • 2
1 Solution
 
dakydCommented:
offsetParent refers to an element's container, and offsetLeft/offsetTop are the offsets relative to that container.  If there is no nesting, the container is the window, so you offsetLeft is the same as the element's x position and offsetTop is the element's y position.  Once you nest your elements, however, that won't work.  Here's a simple example:
<html>
<body>
<div id="outer" style="position: relative; left: 50px; top: 25px;">
  <div id="inner">Hello world</div>
</div>
</body>
</html>

If you alert the offsetLeft of the inner div, you'll get 0, but clearly it's not at the (0, 0) position of the window.  However, inner's offsetLeft + outer's offsetLeft will give you the right answer, because the outer div adds to the inner's overall x-position (relative to the window).  That's the key - so long as the element has a container that isn't the window, you keep adding the offsetLeft.  So a function to do this would look like this:

function getX(obj)
{
  var temp = obj;
  var left = 0;
  while (temp.offsetParent)
  {
      left += temp.offsetLeft;
      temp = temp.offsetParent;
  }
  return left;
}

if you now call getX() on the inner div, you'll get the x-position, relative to the screen, of that element.  The getY() function would be analogous, you just replace offsetLeft with offsetTop.  Hope that helps.
0
 
knightEknightCommented:
my versions of the same functions:


function getX(obj)
{
  return( obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft+getX(obj.offsetParent) );
}

function getY(obj)
{
  return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getY(obj.offsetParent) );
}
0
 
khansoulAuthor Commented:
> temp = temp.offsetParent;

so if the parameter provided was "this" Then offsetParent will keep on moving to next object?

this = this.offsetParent

Thanks a lot

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
khansoulAuthor Commented:
> knightEknight
You gave me this Solution already to one of my question, but this time i was looking for explaination:)
0
 
knightEknightCommented:
oh sorry ... I was hasty this time too, I just got here.  I'll read it over.
0
 
knightEknightCommented:
basically dakyd's functions and mine do the same thing.  Each element exists within the context of its parent element, and each element has an X,Y called offsetTop and offsetLeft.  Our functions sum up these values for each element and its ancenstor elements until there are no more ancestors (parents) and returns the resulting sum.
0
 
dakydCommented:
>> so if the parameter provided was "this" Then offsetParent will keep on moving to next object?

Not sure what you mean by "next object", though I think you've got the right idea.  if you were to actually follow what the value temp holds, it'd look something like this:
iteration1:  temp = this
iteration2:  temp = this.offsetParent
iteration3:  temp = this.offsetParent.offsetParent
iteration4:  temp = this.offsetParent.offsetParent.offsetParent

... and so on and so forth until temp = the object which has no parent itself.  The only element for which that is true is the window.  So basically, you start with your element, and move outward through all the containers until you hit the window.  Along the way, you add up all the offsets (left or top), and the final sum you get is the x or y position relative to the screen.  Hope that helps.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now