2012-06-29 115 views
2

通常讓我用任何元素的窗口位置:獲取元素窗口位置從內浮動DIV

while(el) { 
    x += el.offsetLeft; 
    y += el.offsetTop; 
    el = el.offsetParent; 
} 

不過,我遇到了一個問題,當元素是浮動專區內。

<div id="wrapper"> 
    <div id="floatLeft" style="float: left; width: 20%;"></div> 
    <div id="floatRight" style="float: right; width: 80%;"> 
     <div>I want to find the window pos of this</div> 
    </div> 
</div> 

在上述情況下,元素嵌套在浮動div內,它只返回右邊div的偏移量。當然,這是一個簡化的例子,在現實世界的情況下,正確的div會有多個嵌套元素等。無論如何,我該如何修改我使用的JavaScript來考慮浮動元素?

編輯:問題實際上是我在定位絕對元素的方式,而不是與浮動div的偏移有關。此問題爲空。

回答

0

嘗試使用jQuery:

while(el) { 
    x += $(el).offset().left; 
    y += $(el).offset().top; 
}