有没有一种方法可以适用于所有浏览器?
ask by translate from sooriginal answer
(原始答案)
Yes.
(是。)
window.screen.availHeight
window.screen.availWidth
update 2017-11-10
(更新 2017-11-10)
From Tsunamis in the comments:
(来自海啸的评论:)
To get the native resolution of ie a mobile device you have to multiply with the device pixel ratio:
window.screen.width * window.devicePixelRatio
andwindow.screen.height * window.devicePixelRatio
.(要获得移动设备的原始分辨率,您必须乘以设备像素比率:
window.screen.width * window.devicePixelRatio
和window.screen.height * window.devicePixelRatio
。)This will also work on desktops, which will have a ratio of 1.(这也将在台式机上工作,该台式机的比率为1。)
And from Ben in another answer:
(而来自本的另一个答案是:)
In vanilla JavaScript, this will give you the AVAILABLE width/height:
(在普通JavaScript中,这将为您提供可用的宽度/高度:)
window.screen.availHeight window.screen.availWidth
For the absolute width/height, use:
(对于绝对宽度/高度,请使用:)
window.screen.height window.screen.width