是否有一种方法适用于所有浏览器?


当前回答

function getScreenWidth()
{
   var de = document.body.parentNode;
   var db = document.body;
   if(window.opera)return db.clientWidth;
   if (document.compatMode=='CSS1Compat') return de.clientWidth;
   else return db.clientWidth;
}

其他回答

function getScreenWidth()
{
   var de = document.body.parentNode;
   var db = document.body;
   if(window.opera)return db.clientWidth;
   if (document.compatMode=='CSS1Compat') return de.clientWidth;
   else return db.clientWidth;
}

您还可以获得窗口宽度和高度,避免浏览器工具栏和…(不仅仅是屏幕大小)。

要做到这一点,请使用: 窗口。innerWidth和window。innerHeight属性。详见w3schools。

在大多数情况下,这将是最好的方式,例如,显示一个完美的中心浮动模式对话框。它允许您计算窗口上的位置,无论使用浏览器的分辨率方向或窗口大小。

在JavaScript中,这将为你提供可用的宽度/高度:

window.screen.availHeight
window.screen.availWidth

对于绝对宽度/高度,使用:

window.screen.height
window.screen.width

上面两个代码都可以在没有窗口前缀的情况下编写。

喜欢jQuery吗?这适用于所有浏览器,但每个浏览器给出不同的值。

$(window).width()
$(window).height()

找到屏幕分辨率的简单步骤是:

复制

`My screen resolution is: ${window.screen.width} * ${window.screen.height}`

粘贴到浏览器控制台 回车

使用jQuery,你可以做到:

$(window).width()
$(window).height()