当我滚动父级时,为什么position()。top会发生变化?

时间:2013-12-19 20:25:42

标签: jquery position

jQuery position()返回

  

匹配组中第一个元素的当前坐标   元素,相对于偏移的父元素。

所以,滚动父级是不应该改变位置,对吗?

我在this fiddle中获得的结果是,在将父级滚动100px之后,子元素的position().top会改变100。

  

位置()。滚动1880之前的顶部,滚动1780之后

为什么?

1 个答案:

答案 0 :(得分:0)

要回答评论中的问题,只需将框的scrollTop添加到锚定元素的位置。

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()));
});
相关问题