怎么让层居中

senliy 2008-09-25 03:57:57
怎么让层(DIV)居中
在页面中,任意点击一个按扭或一个链接
弹出一个窗口(层),而这个弹出一个窗口(层)就会在页面的中间显示
...全文
1697 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyq1986 2008-09-26
  • 打赏
  • 举报
回复
用jQuery的plug-in,thickbox.js
l_jiayou 2008-09-26
  • 打赏
  • 举报
回复
<!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=gb2312" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
window.onload=function(){
var test=document.getElementById("test");
var width=parseInt(test.style.width);
var height=parseInt(test.style.height);
test.style.top="50%";
test.style.left="50%";
test.style.marginLeft=(width/2*-1)+"px";
test.style.marginTop=(height/2*-1)+"px";
}
</script>
<body>
<div id="test" style="width:100px;height:50px; position:absolute; border:solid 1px green; top:0px; left:0px;">aaaaaaaaaaaaaaabbbbbbbbb<br />
ccccccccccccddddddddddddddddddddddd
</div>
</body>
</html>
西安风影 2008-09-25
  • 打赏
  • 举报
回复
<html>
<body>
<style>
#warp{
position: absolute;
width:500px;
height:400px;
left:50%;
top:50%;
margin-left:-250px;
margin-top:-200px;
border: solid 1px red;
}
</style>
<body>
<div id="warp" style="display:none"></div>
<input type="button" value="Test" onclick="document.getElementById('warp').style.display='block'" />
</body>
</html>


width:500px;
height:400px;
left:50%;
top:50%;
margin-left:-250px;
margin-top:-200px;
注意margin-left和margin-top分别是width和height的一半可以准确定位在页面中间
king5281273 2008-09-25
  • 打赏
  • 举报
回复
<SCRIPT LANGUAGE="JavaScript">
var s = "网页可见区域宽:"+ document.body.clientWidth;
s += "\r\n网页可见区域高:"+ document.body.clientHeight;
s += "\r\n网页可见区域宽:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "\r\n网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "\r\n网页正文全文宽:"+ document.body.scrollWidth;
s += "\r\n网页正文全文高:"+ document.body.scrollHeight;
s += "\r\n网页被卷去的高:"+ document.body.scrollTop;
s += "\r\n网页被卷去的左:"+ document.body.scrollLeft;
s += "\r\n网页正文部分上:"+ window.screenTop;
s += "\r\n网页正文部分左:"+ window.screenLeft;
s += "\r\n屏幕分辨率的高:"+ window.screen.height;
s += "\r\n屏幕分辨率的宽:"+ window.screen.width;
s += "\r\n屏幕可用工作区高度:"+ window.screen.availHeight;
s += "\r\n屏幕可用工作区宽度:"+ window.screen.availWidth;
alert(s);
</SCRIPT>
liuyilidan 2008-09-25
  • 打赏
  • 举报
回复
事件可以是你自己的事件
liuyilidan 2008-09-25
  • 打赏
  • 举报
回复
var iNodeWidth = parseInt(document.getElementById("对象ID").style.width);
var iNodeHeight= parseInt(document.getElementById("对象ID").style.height);
window.onscroll = function()
{
var iNowLeft = (parseInt(document.body.clientWidth)/2-iNodeWidth/2)+parseInt(document.body.scrollLeft);
var iNowTop = (parseInt(document.body.clientHeight)+/2-iNodeHeight/2)+parseInt(document.body.scrollHeight);
document.getElementById("对象ID").style.left = iNowLeft;
document.getElementById("对象ID").style.top = iNowTop;
}
liuyilidan 2008-09-25
  • 打赏
  • 举报
回复
var iNodeWidth = parseInt(document.getElementById("对象ID").style.width);
var iNodeHeight= parseInt(document.getElementById("对象ID").style.height);
window.onscroll = function()
{
var iNowLeft = (parseInt(document.body.clientWidth)/2-iNodeWidth/2)+parseInt(document.body.scrollLeft);
var iNowTop = (parseInt(document.body.clientHeight)+/2-iNodeHeight/2)++parseInt(document.body.scrollHeight);
document.getElementById("对象ID").style.left = iNowLeft;
document.getElementById("对象ID").style.top = iNowTop;
}
阿云ivan 2008-09-25
  • 打赏
  • 举报
回复
var w=500;//div的宽
var h=600;//div的高
var l=screen.width/2-w/2; //计算出来的居中的left
var t=screen.height/2-h/2; //计算出来的居中的top
dln1234 2008-09-25
  • 打赏
  • 举报
回复
<html>
<hrad>
<style>
div{position:absolute;width:100;height:0;
left:expression((document.body.clientWidth-this.offsetWidth)/2);
top:expression((document.body.clientHeight-this.offsetHeight)/2);}
</style>
<script type="text/javascript">
function Div_cl(){document.getElementById("div_cx").style.display="block";}
function Div_cll(){document.getElementById("div_cx").style.display="none";}
</script>
</head>
<body>
<input type="button" value="我要找找" onclick="Div_cl()"/>
<input type="button" value="我要关掉你" onclick="Div_cll()"/>
<a href="#" onclick="Div_cl()">我要去连接你</a>
<div style="display:none" id="div_cx"><table width="246" height="240" border="1">
<tr>
<td bordercolor="#FF6633" bgcolor="#FFFF00">
</div></td>
</tr>
</table></div>
</body>
</html>
小例子!!!!
senliy 2008-09-25
  • 打赏
  • 举报
回复
刚学,还给点提示吧
谢谢
geek007 2008-09-25
  • 打赏
  • 举报
回复
给点提示,其他的自己写吧
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;

div1.style.marginTop=TopPosition;

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧