这将是更适合 jquery 的东西。您可以调用它来调整加载时的高度和调整窗口大小。
要获得窗口的高度和宽度,您将使用
windowHeight = $(window).height();
windowWidth = $(window).width();
你把它赋值给一个变量,然后减去页眉和页脚的高度。并通过使用设置css
${'#content_container').css({width: windowwidth+"px", height: windowHeight+"px"});
然后,如果有人调整窗口大小,您可以在如下函数中运行相同的选项
$(window).resize(function() {
//update stuff
});
您的代码如下所示,但更改会对每个代码执行相同的操作。
$(document).ready(function(){
windowHeight = $(window).height();
windowWidth = $(window).width();
divHeight = (windowHeight - 100 - 100)/2; // heights of your header/footer
divWidth = windowWidth / 2;
$('#content_container').css({width: divWidth+"px", height: divHeight+"px"});
});
$(window).resize(function() {
windowHeight = $(window).height();
windowWidth = $(window).width();
divHeight = (windowHeight - 100 - 100)/2; // heights of your header/footer
divWidth = windowWidth / 2;
$('#content_container').css({width: divWidth+"px", height: divHeight+"px"});
});
您可以通过分配变量并调用 $('#divid').height(); 来替换 100 的静态高度调用。由于您的标题是位置:绝对,如果您减去标题,那么您需要将 div 从顶部定位到相同的像素。
要调用 js,请在上面的 javascript 之前包含以下内容。
<script src="http://code.jquery.com/jquery-1.6.4.min.js" />