Why is position().top changing when I scroll the parent?

自古美人都是妖i 提交于 2019-12-11 09:23:40

问题


jQuery position() returns

the current coordinates of the first element in the set of matched elements, relative to the offset parent.

So, scrolling the parent is not supposed to change the position, right?

The result I'm getting in this fiddle is that after scrolling the parent by 100px, the position().top of a child element changes by 100.

position().top before scroll 1880, after scroll 1780

Why?


回答1:


To answer the question in your comments, just add the box's scrollTop to the anchored element's position.

http://jsfiddle.net/5xqEL/17/

var $box = $('#box'),
    $anchored = $('#anchored'),
    $debug = $('#debug');

$debug.text('position().top before scroll ' + ($anchored.position().top + $box.scrollTop()));

$box.animate({
    scrollTop: 100
}).promise().then(function () {
    $debug.text($debug.text() + ', after scroll ' + ($anchored.position().top + $box.scrollTop()));
});


来源:https://stackoverflow.com/questions/20691021/why-is-position-top-changing-when-i-scroll-the-parent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!