2012-10-05 59 views

回答

121

添加outerheight到頂部,你有底部,相對於父元素:

var $el = $('#bottom'); //record the elem so you don't crawl the DOM everytime 
var bottom = $el.position().top + $el.outerHeight(true); // passing "true" will also include the top and bottom margin 

有了絕對定位的元素或相對於文件定位時,您將需要自行評估使用偏移:

var bottom = $el.offset().top + $el.outerHeight(true); 

正如trnelson指出,這並不工作時間的100%。要對定位元素使用此方法,還必須考慮偏移量。有關示例,請參閱以下代碼。

var bottom = $el.position().top + $el.offset().top + $el.outerHeight(true); 
+1

。謝謝! – lagwagon223

+1

美!這個作品真棒! – tim687

+6

我提交了一個編輯,但它被拒絕了。在這種情況下,這個答案在技術上是正確的,但並不能100%地解決問題。要使用此方法定位元素,您還必須考慮偏移:'var bottom = $('#bottom')。position()。top + $('#bottom')。offset()。top + $('#bottom ').outerHeight(true)' – trnelson

2
var bottom = $('#bottom').position().top + $('#bottom').height(); 
1

底部是top +的outerHeight不是height,因爲它不包括邊緣或填充。

var $bot, 
    top, 
    bottom; 
$bot = $('#bottom'); 
top = $bot.position().top; 
bottom = top + $bot.outerHeight(true); //true is necessary to include the margins 
4

答案爲止將工作..如果你只想使用高度不填充,邊框等

如果你想佔填充,邊框和邊距,你應該使用.outerHeight

var bottom = $('#bottom').position().top + $('#bottom').outerHeight(true); 
0
var top = ($('#bottom').position().top) + ($('#bottom').height()); 
9

編輯:現在這個方案是在原來的答案了。

接受的答案不完全正確。你不應該使用position()函數,因爲它是相對於父項的。 (?在大多數情況下),如果你正在做全球定位你只應與outerheight添加偏移頂部像這樣:

var actualBottom = $(selector).offset().top + $(selector).outerHeight(true); 

工作就像一個魅力的文檔http://api.jquery.com/offset/