I have this code
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="../css/style.css" /> <style> html,body { width: 100%; margin: 0; padding: 0; } #main { margin: 0 auto; width: 963px; height: 642px; } #preload { display: block; position: absolute; width: 100%; height: 100%; } .loading { position:absolute; top: 43%; left: 47%; z-index: 2; display: none; } </style> </head> <body> <div id="main"> <img id="preload" src="preload.png"/> <img class="loading" src="../effects/loading icon/loading3.gif" /> </div> <script type="text/javascript" src="jquery-1.9.1.min.js"></script> <script type="text/javascript" src="../player/player.js"></script> <script type="text/javascript" src="pic1.js"></script> <script> $(document).ready(function() { $(window).on('resize', function(){ var maxWidth = $(window).width(); var maxHeight = $(window).height(); var ratio = $("#main").height() / $("#main").width(); if(maxWidth * ratio > maxHeight) { $("#main").height(maxHeight); $("#main").width(maxHeight / ratio); } else { $("#main").width(maxWidth); $("#main").height(maxWidth * ratio); } $("#preload").width($("#main").width()); $("#preload").height($("#main").height()); }).trigger('resize'); })(jQuery); </script> </body> </html>
What I want is a div and img inside for automatically resizing with window resizing. The problem is that when loading the 1st half there is a space below the div, like picture 1, and when I change the size of the window, the space disappears, as shown in Figure 2. Why is there a place in picture 1 and how can I fix it, tks so much :) 

Update Jsfiddle: http://jsfiddle.net/7bNjf/
source share