【问题标题】:Obtaining element position relative to the nth parent获取元素相对于第 n 个父元素的位置
【发布时间】:2023-03-13 00:45:02
【问题描述】:

我正在构建一个动态图像滚动器,我需要知道如何调用元素相对于其第 n 个父元素的偏移位置。

有什么想法吗?

【问题讨论】:

  • 获取两个元素的绝对偏移量,然后算出差值?

标签: jquery position offset


【解决方案1】:
function getRelativePosition( element, nthparent_of_element ){
   var posParent = $(nthparent_of_element).position();
   var posElement = $(element).position();
   return { top: posElement.top - posParent.top, 
            left: posElement.left - posParent.left};
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多