我还在学习JavaScript 最最初级的阶段,哪位高手帮我解决一下下面的代码,这段代码是没有问题的,只是效果不是我想要的,我想要的是弹出框的位置不随滚动条而改变,就是一直要出现在窗口的居中位置,位置不变,现在的弹出框每次弹出是弹到首页。请各位大侠帮忙! <script language="javascript">
function BOX_show(e)//显示
{
if(document.getElementByIdx_x(e)==null)
{
return ;
}
var selects = document.getElementsByTagName_r('select');
for(i = 0; i < selects.length; i++)
{
selects[i].style.visibility = "hidden";
} BOX_layout(e);
window.onresize = function(){BOX_layout(e);} //改变窗体重新调整位置
window.onscroll = function(){BOX_layout(e);} //滚动窗体重新调整位置
document.onkeyup = function(event)
{
var evt = window.event || event;
var code = evt.keyCode?evt.keyCode : evt.which;
//alert(code); if(code == 27)
{
BOX_remove(e);
}
} } function BOX_remove(e)//移除
{
window.onscroll = null;
window.onresize = null;
document.getElementByIdx_x('BOX_overlay').style.display="none";
document.getElementByIdx_x(e).style.display="none"; var selects = document.getElementsByTagName_r('select');
for(i = 0; i < selects.length; i++)
{
selects[i].style.visibility = "visible";
}
} function BOX_layout(e)//调整位置
{
var a = document.getElementByIdx_x(e); if (document.getElementByIdx_x('BOX_overlay')==null)//判断是否新建遮掩层
{ var overlay = document.createElement_x("div");
overlay.setAttribute('id','BOX_overlay'); //overlay.onclick=function(){BOX_remove(e);};
//a.parentNode.appendChild(overlay);
document.body.appendChild(overlay);
} document.getElementByIdx_x('BOX_overlay').ondblclick=function(){BOX_remove(e);};
//取客户端左上坐标,宽,高
var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); var clientWidth;
if (windows.innerWidth)
{
clientWidth = windows.innerWidth;
// clientWidth = ((Sys.Browser.agent === Sys.Browser.Safari) ? windows.innerWidth : Math.min(windows.innerWidth, document.documentElement.clientWidth));
}
else
{
clientWidth = document.documentElement.clientWidth;
} var clientHeight;
if (windows.innerHeight)
{
clientHeight = windows.innerHeight;
//clientHeight = ((Sys.Browser.agent === Sys.Browser.Safari) ? windows.innerHeight : Math.min(windows.innerHeight, document.documentElement.clientHeight));
}
else
{
clientHeight = document.documentElement.clientHeight;
} var bo = document.getElementByIdx_x('BOX_overlay');
bo.style.left = scrollLeft+'px';
bo.style.top = scrollTop+'px';
bo.style.width = clientWidth+'px';
bo.style.height = clientHeight+'px';
bo.style.display="";
//Popup窗口定位
a.style.position = 'absolute';
a.style.zIndex=999;
a.style.display="";
//a.style.left = scrollLeft+((clientWidth-a.offsetWidth)/2)+'px';
//a.style.top = scrollTop+((clientHeight-a.offsetHeight)/2)+'px';
} function HiddenButton(e)
{
e.style.visibility='hidden';
e.coolcodeviousSibling.style.visibility='visible'
}
</script>
function BOX_show(e)//显示
{
if(document.getElementByIdx_x(e)==null)
{
return ;
}
var selects = document.getElementsByTagName_r('select');
for(i = 0; i < selects.length; i++)
{
selects[i].style.visibility = "hidden";
} BOX_layout(e);
window.onresize = function(){BOX_layout(e);} //改变窗体重新调整位置
window.onscroll = function(){BOX_layout(e);} //滚动窗体重新调整位置
document.onkeyup = function(event)
{
var evt = window.event || event;
var code = evt.keyCode?evt.keyCode : evt.which;
//alert(code); if(code == 27)
{
BOX_remove(e);
}
} } function BOX_remove(e)//移除
{
window.onscroll = null;
window.onresize = null;
document.getElementByIdx_x('BOX_overlay').style.display="none";
document.getElementByIdx_x(e).style.display="none"; var selects = document.getElementsByTagName_r('select');
for(i = 0; i < selects.length; i++)
{
selects[i].style.visibility = "visible";
}
} function BOX_layout(e)//调整位置
{
var a = document.getElementByIdx_x(e); if (document.getElementByIdx_x('BOX_overlay')==null)//判断是否新建遮掩层
{ var overlay = document.createElement_x("div");
overlay.setAttribute('id','BOX_overlay'); //overlay.onclick=function(){BOX_remove(e);};
//a.parentNode.appendChild(overlay);
document.body.appendChild(overlay);
} document.getElementByIdx_x('BOX_overlay').ondblclick=function(){BOX_remove(e);};
//取客户端左上坐标,宽,高
var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); var clientWidth;
if (windows.innerWidth)
{
clientWidth = windows.innerWidth;
// clientWidth = ((Sys.Browser.agent === Sys.Browser.Safari) ? windows.innerWidth : Math.min(windows.innerWidth, document.documentElement.clientWidth));
}
else
{
clientWidth = document.documentElement.clientWidth;
} var clientHeight;
if (windows.innerHeight)
{
clientHeight = windows.innerHeight;
//clientHeight = ((Sys.Browser.agent === Sys.Browser.Safari) ? windows.innerHeight : Math.min(windows.innerHeight, document.documentElement.clientHeight));
}
else
{
clientHeight = document.documentElement.clientHeight;
} var bo = document.getElementByIdx_x('BOX_overlay');
bo.style.left = scrollLeft+'px';
bo.style.top = scrollTop+'px';
bo.style.width = clientWidth+'px';
bo.style.height = clientHeight+'px';
bo.style.display="";
//Popup窗口定位
a.style.position = 'absolute';
a.style.zIndex=999;
a.style.display="";
//a.style.left = scrollLeft+((clientWidth-a.offsetWidth)/2)+'px';
//a.style.top = scrollTop+((clientHeight-a.offsetHeight)/2)+'px';
} function HiddenButton(e)
{
e.style.visibility='hidden';
e.coolcodeviousSibling.style.visibility='visible'
}
</script>
参考里边怎么计算位置的代码
假设现在是最复杂的情况 多frame页面之间嵌套
遮罩层需要覆盖所有的子页面
这时候你需要遍历每一个子页面
计算每个页面的可视部分宽高 有无滚动条
有的话 需要监听“滚动条拖动的事件”(另外还有窗体拖动的事件不知你有没有考虑)
不断获取溢出的宽度
以此来计算遮罩层的坐标
你可能看过有些网站的浮动广告 那就是这么做的
function openDiv(newDivID) {
var newMaskID = "mask"; //遮罩层id
var newMaskWidth = document.body.scrollWidth; //遮罩层宽度
var newMaskHeight = document.body.scrollHeight; //遮罩层高度
//mask遮罩层
var newMask = document.createElement("div"); //创建遮罩层
newMask.id = newMaskID; //设置遮罩层id
newMask.style.position = "absolute"; //遮罩层位置
newMask.style.zIndex = "1"; //遮罩层zIndex
newMask.style.width = newMaskWidth + "px"; //设置遮罩层宽度
newMask.style.height = newMaskHeight + "px"; //设置遮罩层高度
newMask.style.top = "0px"; //设置遮罩层于上边距离
newMask.style.left = "0px"; //设置遮罩层左边距离
newMask.style.background = "red"; //#33393C//遮罩层背景色
newMask.style.filter = "alpha(opacity=40)"; //遮罩层透明度IE
newMask.style.opacity = "0.40"; //遮罩层透明度FF
document.body.appendChild(newMask); //遮罩层添加到DOM中 //新弹出层
var newDivWidth = 400; //新弹出层宽度
var newDivHeight = 200; //新弹出层高度
var newDiv = document.createElement("div"); //创建新弹出层
newDiv.id = newDivID; //设置新弹出层ID
newDiv.style.position = "absolute"; //新弹出层位置
newDiv.style.zIndex = "9999"; //新弹出层zIndex newDiv.style.width = newDivWidth + "px"; //新弹出层宽度
newDiv.style.height = newDivHeight + "px"; //新弹出层高度
var newDivtop = (document.body.scrollTop + document.body.clientHeight / 2 - newDivHeight / 2); //新弹出层距离上边距离
var newDivleft = (document.body.scrollLeft + document.body.clientWidth / 2 - newDivWidth / 2); //新弹出层距离左边距离
newDiv.style.top = newDivtop + "px"; //新弹出层距离上边距离
newDiv.style.left = newDivleft + "px"; //新弹出层距离左边距离
newDiv.style.background = "#EFEFEF"; //新弹出层背景色
newDiv.style.border = "1px solid #860001"; ///新弹出层边框样式
newDiv.style.padding = "5px"; //新弹出层
newDiv.innerHTML = "content;content"; //新弹出层内容
document.body.appendChild(newDiv); //新弹出层添加到DOM中 //弹出层滚动居中
function newDivCenter() {
newDiv.style.top = (document.body.scrollTop + document.body.clientHeight / 2 - newDivHeight / 2) + "px";
newDiv.style.left = (document.body.scrollLeft + document.body.clientWidth / 2 - newDivWidth / 2) + "px";
}
if (document.all)//处理滚动事件,使弹出层始终居中
{
window.attachEvent("onscroll", newDivCenter);
}
else {
window.addEventListener('scroll', newDivCenter, false);
} //关闭新图层和mask遮罩层 var newA = document.createElement("span");
newA.href = "#";
newA.style.position = "absolute"; //span位置
newA.style.left = 350 + "px";
newA.style.cursor = "pointer"; //span手型
newA.innerHTML = "Close";
newA.onclick = function ()//处理关闭事件
{
if (document.all) {
window.detachEvent("onscroll", newDivCenter);
}
else {
window.removeEventListener('scroll', newDivCenter, false);
}
document.body.removeChild(newMask); //移除遮罩层
document.body.removeChild(newDiv); ////移除弹出框
return false;
}
newDiv.appendChild(newA); //添加关闭span
}
</script>
以前写的一个遮罩层,你改改吧。
pw=弹出框的宽度弹出框.style.left=(sw-pw)/2+'px';上面的文字内容自己去搜索下 怎么获取