Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically resize element with window size jquery

Tags:

jquery

resize

I've looked at many questions regarding this subject and can not seem to find out what is wrong with my code. Any help would be greatly appreciated!

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

I'm trying to resize #element to the same height and width as the window if the window is resized.

like image 325
d-_-b Avatar asked Jul 06 '12 17:07

d-_-b


Video Answer


1 Answers

About .innerWidth(), from docs:

This method is not applicable to window and document objects; for these, use .width() instead.

There is same note for .innerHeight() also.

So you need to use .width() and .height():

$(window).resize(function(){
    var newwidth = $(window).width();
    var newheight = $(window).height();      
    $("#element").height(newheight).width(newwidth);
});
like image 146
Engineer Avatar answered Oct 20 '22 16:10

Engineer