2012-07-06 20 views
5

He examinado muchas preguntas sobre este tema y parece que no puedo encontrar el problema con mi código. ¡Cualquier ayuda sería muy apreciada!Cambiar el tamaño del elemento dinámicamente con el tamaño de la ventana jquery

$(window).resize(function(){ 
    var newwidth = $(window).innerWidth(); 
    var newheight = $(window).innerHeight();  
    $("#element").height(newheight).width(newwidth); 
     }); 

Estoy tratando de cambiar el tamaño de #element a la misma altura y anchura que la ventana si se cambia el tamaño de la ventana.

+1

Esto funciona para mí. – Tyrsius

+1

Vea este violín: http://jsfiddle.net/aBHRp/1/ – Tyrsius

+0

Ayúdeme, http://stackoverflow.com/questions/33360273/window-disptachevent-resizing-issue –

Respuesta

13

Sobre .innerWidth(), desde docs:

Este método no es aplicable a los objetos de la ventana y de documentos; para estos, use .width() en su lugar.

Hay el mismo note para .innerHeight() también.

Así que hay que utilizar .width() y .height():

$(window).resize(function(){ 
    var newwidth = $(window).width(); 
    var newheight = $(window).height();  
    $("#element").height(newheight).width(newwidth); 
}); 
+0

gracias por la respuesta. Es interesante porque si alerta (newwidth) obtengo el ancho correcto. –

+1

Tanto el ancho como el ancho interno funcionan para mí. – Tyrsius

+0

@Tyrsius Tal vez haya un problema de compatibilidad entre navegadores. – Engineer

1

intente esto:

$(window).resize(function(){ 
    var newwidth = $(window).width(); 
    var newheight = $(window).height();  
    $("#element").css({"height": newheight, "width": newwidth }); 
}); 
+1

gracias, esta es otra manera que ahora funcionó para mí! –

+0

@iight eres bienvenido :) – undefined

1

siempre puede utilizar CSS:

#element {width:100%; height:100%; position:fixed; } 
+0

voto a favor? esto hace lo mismo que tiene Op en su código, excepto sin el jQuery. –

1

En la llanura Javascript que puede hacer esto:

var viewportwidth; 
    var viewportheight; 

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 

    if (typeof window.innerWidth != 'undefined') 
    { 
     viewportwidth = window.innerWidth, 
     viewportheight = window.innerHeight 
    } 

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document) 

    else if (typeof document.documentElement != 'undefined' 
    && typeof document.documentElement.clientWidth != 
    'undefined' && document.documentElement.clientWidth != 0) 
    { 
     viewportwidth = document.documentElement.clientWidth, 
     viewportheight = document.documentElement.clientHeight 
    } 

    // older versions of IE 

    else 
    { 
     viewportwidth = document.getElementsByTagName('body')[0].clientWidth, 
     viewportheight = document.getElementsByTagName('body')[0].clientHeight 
    } 
    var mydiv = document.getElementById("element"); 
    mydiv.style.height=viewportheight'px'; 
    mydiv.style.width=viewportwidth+'px'; 
1

jsFiddle

$(window).resize(function(){ 
var newwidth = $(window).width(); 
var newheight = $(window).height();  
$("#element").height(newheight).width(newwidth); 
    });​ 

ambas obras para mí

update

$(window).resize(function(){ 
var newwidth = $(window).innerWidth(); 
var newheight = $(window).innerHeight();  
$("#element").height(newheight).width(newwidth); 
    });​ 
Cuestiones relacionadas