![](/img/trans.png)
[英]Absolute positioning + translate loop is positioning related to sibling, not relative position parent?
[英]positon element relative to parent.parent container in JS? no absolute/fixed positioning?
我有一個棘手的問題,我正拼命試圖找到一個可行的解決方案。
article#post
和aside#sidebar
左右並排浮動-非常簡單。 在我的article#post
內部有一個h1
和一個div.post-entry
。
我想知道是否可以確定div.post-entry
與外部容器section#content
的相對偏移section#content
。 我要這樣做的原因是,我可以將浮動的側邊欄margin-top
留在與“ div.post-entry”相同的高度上。
這是我當前的html結構的模型:
<section id="content">
<article id="post">
<h1>Title of the post</h1>
<div class="post-entry">
<p>Something</p>
</div>
</article>
<aside id="sidebar>
Something
</aside>
</section>
再說一次,我想找到任何解決方案來獲取距div.post-entry
位於section#content
內部多遠的像素值。
我的主要問題是,我什至似乎都無法post-entry.offsetTop
正常工作。 這應該是div.post-entry
相對於article.post
的偏移量,因為這是它的父級。 這實際上也應該對我有用。
var articleOffset = $('.post-entry').offsetTop;
console.log($('.post-entry')); //found and exists
console.log(articleOffset); undefined
有什么想法為什么這也不適合我嗎? 為什么我在這里對offsetTop
值未定義。 有一個很大的<h1>
,其填充至少為“ 30px”,可以偏移div.post-entry
。
有什么想法嗎? 也許還有其他很酷的解決方案呢? 我只希望aside#sidebar
從與div.post-entry
相同的高度開始,但是我不想更改標記。
您需要從jQuery對象中選擇DOM元素。
var articleOffset = $('.post-content')[0].offsetTop;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.