2011-09-11 20 views
57

¿Cómo determino scrollHeight de una división use css overflow: auto?¿Cómo determino scrollHeight?

que he probado:

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

En última instancia, estoy tratando de crear una charla y siempre tienen la barra de desplazamiento para el mensaje actual en la pantalla.

así que estaba pensando algo como lo siguiente:

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

Gracias,

Brian

+0

¿Qué hay de malo con el uso de scrollHeight()? –

+2

@BadrHari: no hay función 'scrollHeight()' en jQuery. –

+0

es $ ('prueba'). Get (0) .scrollHeight(); – JFouad

Respuesta

57

scrollHeight es una propiedad Javascript a regular, de manera que no es necesario jQuery.

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

Gracias! Esto funcionó para mí: var height = document.getElementById ("chatLog"). ScrollHeight - $ ('# chatLog'). Height(); \t \t $ ('# chatLog'). ScrollTop (alto); – Brian

+6

¿Qué hay de la compatibilidad del navegador? ¿Todas las versiones principales de navegadores lo admiten? IE8 +? – SexyBeast

+1

@Cupidvogel La página MDN vinculada dice IE8 + – Dennis

212

formas correctas en jQuery son -

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

espero que ayude.

+10

+1 por $ ('# test') [0] .scrollHeight, dado que el asker ya estaba usando jQuery. – Jackson

+9

Esta debería ser la respuesta aceptada ya que responde la pregunta. – invot

+0

Debe ser la respuesta aceptada. Tenga en cuenta que el método 'prop' requiere jquery versión 1.6 o superior. +1 de mí – Vayne

Cuestiones relacionadas