请问一下,我想弹出一个div,但是此div需要显示在页面的正中央,请问该怎么定义属性 请问一下,我想弹出一个div,但是此div需要显示在页面的正中央(无论滚动过几屏),请问该怎么计算top属性现在该div已经可以弹出了left属性我已经用window.screen.width计算出来了但是top属性,我该怎么计算,请帮助,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 window.screen.height 也有这个属性 啊 然后除以2 在减去你的层的高度除以2 然后 加上 document.body.scrollTop <!doctype html><html> <head> <meta charset="gb2312" /> <title>Test</title> <style> body { height:2000px; } #test { position:absolute; background:#ddd; } </style> <script> function $(o){return document.getElementById(o)} window.onload = function(){ var obj = $('test'); var sWidth = document.documentElement.scrollWidth; var sHeight = document.documentElement.clientHeight; obj.style.left = sWidth/2-parseInt(obj.style.width)/2+'px'; obj.style.top = sHeight/2-parseInt(obj.style.height)/2+'px'; window.onscroll = function(){ var iTop = document.documentElement.scrollTop + sHeight/2-parseInt(obj.style.height)/2+'px'; obj.style.top = iTop; } } </script> </head> <body> <div id="test" style="width:100px; height:100px;"></div> </body></html>楼主试试 求一个类似于jquery的slideup,slidedown效果 帮忙改一个下拉导航.泪谢 Javascript代码大小的问题 如何用javascript控制表单字体的颜色? 允许用汉字的用户名只能由3-20个字符的汉字(算两个字符)、a-z的英文字母、0-9的数字、@、点或下划线组成,不得包含空格、单引号等非法 JS 字符串转换为json格式 下拉框选项问题,100分题 需要一段网页自动刷新的代码 javascript可以将字符串看成二进制字符做位操作吗? 根据若干路径动态生成JSON数据 js如何判断注册表单的关联。 FireFox的event对象的问题?
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title>Test</title>
<style>
body { height:2000px; }
#test {
position:absolute; background:#ddd;
}
</style>
<script>
function $(o){return document.getElementById(o)}
window.onload = function(){
var obj = $('test');
var sWidth = document.documentElement.scrollWidth;
var sHeight = document.documentElement.clientHeight;
obj.style.left = sWidth/2-parseInt(obj.style.width)/2+'px';
obj.style.top = sHeight/2-parseInt(obj.style.height)/2+'px';
window.onscroll = function(){
var iTop = document.documentElement.scrollTop + sHeight/2-parseInt(obj.style.height)/2+'px';
obj.style.top = iTop;
}
}
</script>
</head>
<body>
<div id="test" style="width:100px; height:100px;"></div>
</body>
</html>
楼主试试