请教一个弹出 DIV 定位的问题。 这样是不行的。我上面的代码就已经是取屏幕高度了。如果下拉滑动条发生变化,div 的 top 还是不变的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用event.x event.y 试试.. 或者 event.clienty 加一句:document.body.onscroll=function() { div_content.style.top = document.documentElement.scrollTop+windows.screen.height/2; } 我给的代码经过测试一定能用,拿分了!~<!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" /><style type="text/css"></style><title>无标题文档</title><script language="JavaScript" type="text/javascript">function load(){ var div_test = document.getElementById("div_center"); var doc_element = document.documentElement; div_test.style.left = document.body.clientWidth/2 - parseInt(div_test.style.width)/2; div_test.style.top = doc_element.clientHeight/2 - parseInt(div_test.style.height)/2;}function scroll(){ var div_test = document.getElementById("div_center"); var doc_element = document.documentElement; div_test.style.left = document.body.clientWidth/2 - parseInt(div_test.style.width)/2; //div_test.style.top = document.body.clientHeight/2 - parseInt(div_test.style.height)/2; div_test.style.top = doc_element.clientHeight/2 - parseInt(div_test.style.height)/2 + doc_element.scrollTop;}window.onscroll = scroll;</script></head><body onload="load()"><script language="JavaScript" type="text/javascript"> for(var i=0;i<20;i++) { for(var j=0;j<100;j++){ document.write("测试"+j); } document.write("<br>"); } </script><div id="div_center" style="border:solid;position:absolute;width:200px;height:200px;left:50%;font-size:20px;background-color :red;"></div></body></html> JavaScript冲突问题 在官网下的example,打开tree里面得例子,怎么报错说是拒绝访问呢?请问什么问题? 跪求帮忙, 在Safari中动态创建下拉列表时所出现的问题 Ext问题: 求个脚本,将select的text传送到焦点的文本框 JS高手请进来指教 初学者关于javascript的一个疑问 HTML编辑器怎样把焦点所在的文本是什么样式,比如:是粗体,粗体按钮就是按中的 日期校验 VUE和Bootstrap使用Table的一个问题 如何在表单提交时验证test开头或结尾有无空格。诚心求解 JS里面有什么办法能把 事件提前到 onload前?在线等~~谢谢
或者 event.clienty
document.body.onscroll=function()
{
div_content.style.top = document.documentElement.scrollTop+windows.screen.height/2;
}
<!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" />
<style type="text/css">
</style>
<title>无标题文档</title>
<script language="JavaScript" type="text/javascript">
function load(){
var div_test = document.getElementById("div_center");
var doc_element = document.documentElement;
div_test.style.left = document.body.clientWidth/2 - parseInt(div_test.style.width)/2;
div_test.style.top = doc_element.clientHeight/2 - parseInt(div_test.style.height)/2;
}
function scroll(){
var div_test = document.getElementById("div_center");
var doc_element = document.documentElement;
div_test.style.left = document.body.clientWidth/2 - parseInt(div_test.style.width)/2;
//div_test.style.top = document.body.clientHeight/2 - parseInt(div_test.style.height)/2;
div_test.style.top = doc_element.clientHeight/2 - parseInt(div_test.style.height)/2 + doc_element.scrollTop;
}
window.onscroll = scroll;
</script>
</head>
<body onload="load()">
<script language="JavaScript" type="text/javascript">
for(var i=0;i<20;i++)
{
for(var j=0;j<100;j++){
document.write("测试"+j);
}
document.write("<br>");
}
</script>
<div id="div_center" style="border:solid;position:absolute;width:200px;height:200px;left:50%;font-size:20px;background-color :red;"></div>
</body>
</html>