javascript - Dynamically resize a div using jQuery -


i have div (id="maindiv") need dynamically resize if user changes size of browser window. have written following code try , work doesn't seem setting height of div:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script>     var height = $(window).height();      $("#maindiv").height(height); </script> <body>     <div id="maindiv"></div> </body> 

i don't know jquery, making obvious mistake?

there few things going wrong here:

  1. your code execute straight away before document has finished loading.
  2. your code execute on load of the script, not on resize
  3. you're not setting height of maindiv - you're setting height of element id: accordianmain (but i'm guessing know that).

you need handle browser resize event wait browser resize dimensions of window , apply accordingly. you'd handling window.resize event liks this:

var $win = $(window);  $win.on('resize',function(){     $("#maindiv").height($win.height()); }); 

what want wait for resize finish adding timeout doesn't fire well:

var timer,     $win = $(window);   $win.on('resize',function(){     cleartimeout(timer);     timer = settimeout(function(){         $("#maindiv").height($win.height());     }, 500);  }); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -