滚动父级时,为什么position().top会发生变化? [英] Why is position().top changing when I scroll the parent?

查看:228
本文介绍了滚动父级时,为什么position().top会发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery position() 返回

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?

我进入此小提琴的结果是,将父级滚动100像素后,子元素的position().top更改为100.

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在滚动1880之前,在滚动1780之后

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

为什么?

推荐答案

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

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()));
});

这篇关于滚动父级时,为什么position().top会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆