admin管理员组

文章数量:1390749

Is There any way to find an element position related to latest relative position parent?

I want a javascript way

assume we have this HTML

<div id="div1" style="position:relative"> 
   <div id="div2" style="padding:10px">
      <div id="div3" style="position:absolute top:15px; left:20px;">Stack</div>
   </div>
</div>

I want position of "div3" related to "div1".

Thanks

Is There any way to find an element position related to latest relative position parent?

I want a javascript way

assume we have this HTML

<div id="div1" style="position:relative"> 
   <div id="div2" style="padding:10px">
      <div id="div3" style="position:absolute top:15px; left:20px;">Stack</div>
   </div>
</div>

I want position of "div3" related to "div1".

Thanks

Share Improve this question edited Aug 14, 2016 at 8:23 Mosijava asked Feb 26, 2012 at 15:37 MosijavaMosijava 4,2094 gold badges31 silver badges40 bronze badges 5
  • 1 You want to find the x/y coordinates of div3 in relation to div1? – David Thomas Commented Feb 26, 2012 at 15:40
  • 1 I assume you have tried .position()? api.jquery./position – Felix Kling Commented Feb 26, 2012 at 15:43
  • yes David, To solve a problem That there is in niceform – Mosijava Commented Feb 26, 2012 at 15:43
  • @FelixKling, Is there any java script way? I prefer not to use jQuery – Mosijava Commented Feb 26, 2012 at 15:45
  • Ops, I saw it tagged jQuery for some reason :) In any case, you can have look at the source code for this function. It will give you a cross-browser solution (jQuery is JavaScript too ;)), though it might be a bit over the top. – Felix Kling Commented Feb 26, 2012 at 15:46
Add a ment  | 

1 Answer 1

Reset to default 7

div3.offsetTop and div3.offsetLeft should fit your requirement(s).

本文标签: find position of an element relative to latest relative positioned parent with javascriptStack Overflow