js实现div居中

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>test</title>
<meta name="keywords" content=""/>
<meta name="description" content=""/>
<link type="text/css" rel="stylesheet" href="http://j.laohe360.net/css/pub.css"/>
<script charset="utf-8" type="text/javascript" src="http://www.php100.com/statics/js//php100/js/jquery.js"></script>
<script>
$(document).ready(function () {
	//alert(document.documentElement.clientHeight);
	//alert(document.documentElement.clientWidth);
	var iWidth=500; //弹出窗口的宽度;
	var iHeight=400; //弹出窗口的高度;
	var iTop = (document.documentElement.clientHeight-iHeight)/2; //获得窗口的垂直位置;
	var iLeft = (document.documentElement.clientWidth-iWidth)/2; //获得窗口的水平位置;
	$("#tpsp").height(iHeight)
		.width(iWidth)
		.css('position','absolute')
		.css("top",iTop)
		.css("left",iLeft)
		.css("background",'red');
});

</script>
</head>
<body>


<div id="tpsp">
	
</div>


</body>
</html>
原文地址:https://www.cnblogs.com/yuwensong/p/4147558.html