【问题标题】:jquery offset and fixed position bugjquery偏移和固定位置错误
【发布时间】:2011-12-11 10:41:22
【问题描述】:

我发现.offset() 将相对于父元素,如果它具有 CSS 属性 position:fixed;

这是demo

但应该是相对于文档的吧?

【问题讨论】:

    标签: jquery position offset fixed


    【解决方案1】:

    您的代码错误。 jQuery 偏移量是相对于文档的。将您的 javascript 更改为:

    $(document).ready(function(){
        $('#child').click(function(){
            alert($(this).offset().top);
        });
    });
    

    【讨论】:

      【解决方案2】:

      您的示例不使用 .offset() ...它使用 DOM 元素的 offsetTop。我 changed the example 到 .offset() 并且它是相对于文档的,正如预见的那样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多