0

我想创建一个弹出窗口,并且无论浏览器或屏幕大小如何,我都希望它始终位于中间。

所以为此,我使用的是 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);
   }

线路document.getElementById.('div_register').style.left= ((window.innerWidth)-500)/20;有问题。

4

1 回答 1

0

window.innerWidth不是跨浏览器兼容的,所以它不适用于每个浏览器,所以你需要这样的功能

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};
}
于 2012-04-09T08:04:34.180 回答