是否有一种方法适用于所有浏览器?
当前回答
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。
在大多数情况下,这将是最好的方式,例如,显示一个完美的中心浮动模式对话框。它允许您计算窗口上的位置,无论使用浏览器的分辨率方向或窗口大小。
想要在移动设备上实现这个功能需要更多的步骤。屏幕上。不管设备的方向如何,availWidth保持不变。
以下是我的手机解决方案:
function getOrientation(){
return Math.abs(window.orientation) - 90 == 0 ? "landscape" : "portrait";
};
function getMobileWidth(){
return getOrientation() == "landscape" ? screen.availHeight : screen.availWidth;
};
function getMobileHeight(){
return getOrientation() == "landscape" ? screen.availWidth : screen.availHeight;
};
使用jQuery,你可以做到:
$(window).width()
$(window).height()
找到屏幕分辨率的简单步骤是:
复制
`My screen resolution is: ${window.screen.width} * ${window.screen.height}`
粘贴到浏览器控制台 回车
var width = screen.width;
var height = screen.height;