//弹出窗口
function TanChu(){
$("body").append("<div id='tishi' style=\"background-color:#FFCCCC;position:absolute;text-align:center;top:20px;left:30px;width:150px;height:80px; line-height:80px;\">增加成功!</div>");
$("#tishi").css({top:$(document).height()/2,left:document.documentElement.scrollWidth/2});
}无论浏览器窗口的大小怎么变,弹出的div层始终都在浏览器的中心位置,红色部分应该怎么写,上面的那个不行?
function TanChu(){
$("body").append("<div id='tishi' style=\"background-color:#FFCCCC;position:absolute;text-align:center;top:20px;left:30px;width:150px;height:80px; line-height:80px;\">增加成功!</div>");
$("#tishi").css({top:$(document).height()/2,left:document.documentElement.scrollWidth/2});
}无论浏览器窗口的大小怎么变,弹出的div层始终都在浏览器的中心位置,红色部分应该怎么写,上面的那个不行?
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<style type="text/css">
div{font-family:verdana; font-size:12px; padding:2px; margin:5px; border:1px solid red; height:30px;}
</style>
<script type="text/javascript">
function selectScreen(){
var w,h;
srn_w=screen.width;
srn_h=screen.height;
alert(srn_w/2 + "-" + srn_h/2);//中心点,然后减去你的div的宽度/2和高度/2
}
selectScreen();
</script>
<body>
<br>
<br>
居中显示
</body>
</html>
srn_w=document.body.offsetWidth;
srn_h=document.body.offsetHeight;这样才是,screen.width这个应该是屏幕的宽,不是浏览器的宽。
问题自己已经解决,散分了function TanChu3(){
$("body").append("<div id='tishi' style=\"background-color:#FFCCCC;position:absolute;text-align:center;width:150px;height:80px; line-height:80px;\">收藏成功!</div>");
$("#tishi").css("top",$(window).height()/2-40).css("left",$(window).width()/2-70);
}