是否有一种方法适用于所有浏览器?
当前回答
原来的答案
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
其他回答
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;
}
原来的答案
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
如果你指的是浏览器分辨率的话
窗口。innerWidth为您提供浏览器分辨率
您可以使用http://howbigismybrowser.com/进行测试 尝试通过放大/缩小浏览器来改变屏幕分辨率,并使用http://howbigismybrowser.com/检查分辨率大小 窗口。innerWidth应该与屏幕分辨率宽度相同
找到屏幕分辨率的简单步骤是:
复制
`My screen resolution is: ${window.screen.width} * ${window.screen.height}`
粘贴到浏览器控制台 回车
var width = screen.width;
var height = screen.height;