[英]Setting pop-window at middle based on window.innerWidth
I want to create a pop-up and I want to get it always positioned at the middle, no matter what the browser or screen-size be.我想创建一个弹出窗口,并且无论浏览器或屏幕大小如何,我都希望它始终位于中间。
So for that, I am using window.innerWidth, but I don't know why this code isn't working.因此,为此,我使用的是 window.innerWidth,但我不知道为什么这段代码不起作用。
function show_update_profile()
{
document.getElementById('black_fade').style.display='block';
alert(window.innerWidth);
document.getElementById('div_register').style.display='block';
document.getElementById.('div_register').style.left= ((window.innerWidth)-500)/20;
alert(window.innerWidth);
}
The line document.getElementById.('div_register').style.left= ((window.innerWidth)-500)/20;
行
document.getElementById.('div_register').style.left= ((window.innerWidth)-500)/20;
is having the problem.有问题。
window.innerWidth
is not cross browser compatible so it will not work with every browser, so you need a function like this window.innerWidth
不兼容跨浏览器,因此它不适用于所有浏览器,因此您需要这样的 function
function getBrowserInnerSize(w)
{
var x,y;
if (!w){
w = window;
}
if (w.innerHeight){
// FireFox
x = w.innerWidth;
y = w.innerHeight;
} else if (w.document.documentElement && w.document.documentElement.clientHeight) {
// IE Strict mode
x = w.document.documentElement.clientWidth;
y = w.document.documentElement.clientHeight;
} else if (w.document.body) {
// IE Normal mode
x = w.document.body.clientWidth;
y = w.document.body.clientHeight;
}
return {"x":x,"y":y};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.