当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Get element offset().top when elements are added and removed from the page

问题描述:

I have a div container element that can be at any position on the page. For example, a user might add my element halfway down the page, or 2/3's of the way down, or some arbitrary position. There will be content before/after/before & after it.

I need to get the offset().top of this but I find that this doesn't change. I have a div that prints a line at the point at which the offset is being calculated, but it's nowhere near the ACTUAL top of the div element.

I'm not sure what I'm doing wrong.

The jQuery:

var elementPos;

$(window).load(function () {

// Store the offset

elementPos= $('.elementPosWrapper').offset().top;

});

The basic html:

<div class="elementPosWrapper" id="elementPosWrapper">

...

</div>

With other div elements that may be added before/after this. I just need to have the actual top value of the element be accurate, whether the page is 10,000px or 2,000px high.

分享给朋友:
您可能感兴趣的文章:
随机阅读: