问题:下面代码让方框垂直居中。    从IIS网站访问没问题。  
但是如果在windows文件夹下双击用IE打开HTML时候就有问题, 我在IE8下测试的有问题 (再次刷新网页就好了) 我只想知道啥原因?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题文档</title> 
<script type="text/javascript" src="jquery.js"></script> 
<style type="text/css">#my { width:100px; height:100px; background:#ccc; position:absolute;}
</style>
<script type="text/javascript"> 
$(function(){ var top = (document.documentElement.clientHeight  - $("#my").outerHeight()) / 2;
$("#my").css({ "top":  top + $(document).scrollTop() });
})</script> 
</head> 
<body>
<div id="my"></div>
<div style="height:1900px;"></div>
</body>
</html> 
       

解决方案 »

  1.   

    width: 500px;
    height:400px;
    left:50%;
    top:50%;
    margin-left:-220px!important;/*FF IE7 该值为本身宽的一半 */
    margin-top:-160px!important;/*FF IE7 该值为本身高的一半*/
    margin-top:0px;
    position:fixed!important;/* FF IE7*/
    position:absolute;/*IE6*/
    _top:       expression(eval(document.compatMode &&
                document.compatMode=='CSS1Compat') ?
                documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2 :/*IE6*/
                document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2);/*IE5 IE5.5*/}