aaronstacy
aaronstacy

Reputation: 6428

Calculate position regardless of containing block

I've got a <table> within a couple <div> tags:

<div class=outer>
  <div class=inner>
    <table>
      <tbody>
        <tr><td>col1</td><td>col2</td></tr>
        <tr><td>col1</td><td>col2</td></tr>
      </tbody>
    </table>
  </div>
</div>

If get the position of a row w/ jQuery's $.fn.position function:

console.log($('tr:first').position().top);

It will return a different value depending upon whether I set div.inner to position: relative (example). I believe this is because the row's containing block is different in each case.

I need to calculate the row's position relative to the body regardless of wether div.inner's position is set to relative. Is there an elegant way to accomplish this? Or will I need to ascend the DOM tree, checking whether each ancestor is position: relative, and adding an offset based on that?

Upvotes: 1

Views: 92

Answers (1)

Todd Baur
Todd Baur

Reputation: 1005

I think you console.log($('tr:first').offset().top);

Upvotes: 1

Related Questions