2012-10-05 17 views
45

divがあり、下部の位置を探したい。私はこのような部門のトップポジションを見つけることができますが、どのように私は下部の位置を見つけるのですか?jqueryでdivの下部の位置を見つける

var top = $('#bottom').position().top; 
return top; 

答えて

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 

またはドキュメントへの相対的な位置するとき、あなたが代わりに評価する必要があります。オフセットの使用:trnelsonによって指摘、これは100%の時間を動作しません

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

通り。配置された要素にこのメソッドを使用するには、オフセットも考慮する必要があります。例については、次のコードを参照してください。それはマージンやパディングを含まないように

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

が魅力のように働きました。ありがとう! – lagwagon223

+1

Beauty !!これは素晴らしいです! – 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

EDIT:このソリューションは、あまりにも、元の答えになりました。

回答が正しいとは限りません。 position()関数は親に対して相対的なので、使用しないでください。あなたが唯一そうのようなouterheightと上部オフセットを追加する必要があります(?ほとんどの場合)、全地球測位を行っている場合:

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

ドキュメントhttp://api.jquery.com/offset/

関連する問題