2011-09-11 19 views
57

如何確定一個部門的scrollHeight使用CSS溢出:自動?如何確定scrollHeight?

我已經試過:

$('test').scrollHeight(); 
$('test').height(); but that just returns the size of the div not all the content 

最後,我想創造一個聊天,總是有滾動條在屏幕上的當前消息。

所以我想類似如下:

var test = $('test').height(); 
$('test').scrollTop(test); 

謝謝

布賴恩

+0

使用scrollHeight()有什麼問題? –

+2

@BadrHari:jQuery中沒有'scrollHeight()'函數。 –

+0

它是$('test')。get(0).scrollHeight(); – JFouad

回答

57

scrollHeight是一個普通的JavaScript屬性,所以你並不需要jQuery的。 jQuery中

var test = document.getElementById("foo").scrollHeight; 
+0

謝謝!這對我有效:var height = document.getElementById(「chatLog」)。scrollHeight - $('#chatLog')。height(); \t \t $('#chatLog')。scrollTop(height); – Brian

+6

瀏覽器支持如何?所有主要版本的瀏覽器都支持它? IE8 +? – SexyBeast

+1

@Cupidvogel對於$('#test')[0] .scrollHeight,鏈接的MDN頁面表示IE8 + – Dennis

212

正確的方式是 -

  • $('#test').prop('scrollHeight')
  • $('#test')[0].scrollHeight

希望它能幫助。

+10

+1,因爲提交者已經在使用jQuery。 – Jackson

+9

這應該是被接受的答案,因爲它回答了問題。 – invot

+0

它應該是被接受的答案。請注意'prop'方法需要jquery 1.6或更高版本。從我的+1 – Vayne