简体   繁体   English

将div与顶部相同的偏移对齐

[英]Align div in the same offset top than another

My question is pretty simple. 我的问题非常简单。 I have div1 that has a variable offset().top depending on other elements of the page. 我有div1 ,它有一个变量offset().top取决于页面的其他元素。 I want to insert an absolute div next to it (basically with the same absolute top ) either with just css or with javascript too. 我想在它旁边插入一个absolute div(基本上使用相同的绝对top ),或者仅使用css或使用javascript。

在此输入图像描述

HTML looks something like HTML看起来像

<div>
   <div id="div2">stuff</div>
</div>
<div>
   <div>some divs</div>
   <div id="div1">div1 stuff </div>
</div>

在此输入图像描述

#div1, #div2 { display:inline-block; }

and

<div id="div3">
  <div>
    <div id="div2">stuff</div>
  </div>
  <div>
    <div>some divs</div>
    <div id="div1">div1 stuff </div>
  </div>
</div>

因为在评论中你说你想在div2插入DOM之后放置它,你可以尝试以下方法:

$('#div2').css('top', $('#div1').offset().top - $('#div2').offset().top)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM