是否有一种方法适用于所有浏览器?
当前回答
var width = screen.width;
var height = screen.height;
其他回答
想要在移动设备上实现这个功能需要更多的步骤。屏幕上。不管设备的方向如何,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;
};
原来的答案
Yes.
window.screen.availHeight
window.screen.availWidth
更新2017-11-10
来自海啸的评论:
为了获得移动设备的原生分辨率,你必须与设备像素比相乘:window.screen.width * window.devicePixelRatio和window.screen.height * window.devicePixelRatio。这也适用于桌面,桌面的比率为1。
本的另一个回答是:
在JavaScript中,这将为你提供可用的宽度/高度: window.screen.availHeight window.screen.availWidth 对于绝对宽度/高度,使用: window.screen.height window.screen.width
找到屏幕分辨率的简单步骤是:
复制
`My screen resolution is: ${window.screen.width} * ${window.screen.height}`
粘贴到浏览器控制台 回车
如果你指的是浏览器分辨率的话
窗口。innerWidth为您提供浏览器分辨率
您可以使用http://howbigismybrowser.com/进行测试 尝试通过放大/缩小浏览器来改变屏幕分辨率,并使用http://howbigismybrowser.com/检查分辨率大小 窗口。innerWidth应该与屏幕分辨率宽度相同
请参见使用Javascript获取监视器屏幕分辨率和窗口。屏幕上的对象