Foros del Web » Programando para Internet » Javascript »

ancho y alto de elemento oculto

Estas en el tema de ancho y alto de elemento oculto en el foro de Javascript en Foros del Web. Hola foro: existe alguna manera de poder encontrar el ancho y alto de un elemento oculto, es decir saber cual es el espacio q ocupará ...
  #1 (permalink)  
Antiguo 04/08/2009, 15:44
Avatar de killerangel  
Fecha de Ingreso: septiembre-2007
Ubicación: Un punto en el espacio
Mensajes: 592
Antigüedad: 16 años, 7 meses
Puntos: 10
ancho y alto de elemento oculto

Hola foro:

existe alguna manera de poder encontrar el ancho y alto de un elemento oculto, es decir saber cual es el espacio q ocupará cuando sea visible?

podría mostrarlo, encontrar el tamaño y volver a ocultarlo... pero q pasa si no es el objeto quien esta oculto sino uno de sus contenedores?

a ver si alguien me da una mano en esto q me esta rompiendo la cabeza.
__________________
Sueñen... y trabajen por hacer esos sueños realidad... de eso se construye el futuro!!
Me siento entre la ASP-ada y la PHP-red
  #2 (permalink)  
Antiguo 04/08/2009, 17:02
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 19 años, 11 meses
Puntos: 834
Respuesta: ancho y alto de elemento oculto

Probá algo así:
Código:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
<style>
#contenedor{width:300px; display:none}
</style>
<script>
function css(el,prop){
    if(window.getComputedStyle){
        return document.defaultView.getComputedStyle(document.getElementById(el),null).getPropertyValue(prop);
    }else{
        var re = /(-([a-z]){1})/g;
        if (prop == 'float') prop = 'styleFloat';
        if (re.test(prop)) {
            prop = prop.replace(re, function () {
                return arguments[2].toUpperCase();
            });
        }
        return document.getElementById(el).currentStyle[prop] ? document.getElementById(el).currentStyle[prop] : null;
    }
} 
function medidas(el){
	var test=document.getElementById(el).cloneNode(true);
	test.style.position='absolute';
	test.style.left='-1500px';
	var ancho=parseInt(css(el,'width'))>0?css(el,'width'):css(document.getElementById(el).parentNode.id,'width');
	test.style.width=ancho;
	document.getElementsByTagName('body')[0].appendChild(test);
	var medidas={w:test.offsetWidth,h:test.offsetHeight}
	document.getElementsByTagName('body')[0].removeChild(test);
	return medidas;
}
</script>
</head>

<body>
<div id="contenedor">
  <div id="elemento">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
</div>

<div onclick="var m=medidas('elemento');alert('w:'+m.w+'--h:'+m.h)" style="background-color:#CCC; padding:3px; text-align:center; width:150px; cursor:pointer; font-size:12px">averiguar medidas de elemento</div>
</body>
</html>
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:59.