根据 window.innerWidth 在中间设置弹出窗口

Setting pop-window at middle based on window.innerWidth

本文关键字:窗口 设置 在中间 window innerWidth 根据      更新时间:2023-09-26

我想创建一个弹出窗口,我想让它始终位于中间,无论浏览器或屏幕尺寸如何。

为此,我正在使用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;有问题。

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};
}