我在想是否存在一种方法可以在 JS 中获取屏幕的实际大小(屏幕分辨率)。
- 我知道
screen
API,但它的结果不是我想要的。
screen;
// Screen {availWidth: 1680, availHeight: 973, width: 1680, height: 1050, colorDepth: 30, …}
screen.width;
// 1680
screen.height;
// 1050
我得到的是宽度: 1680,高度: 1050
;
实际上,屏幕尺寸为2880 x 1800
;
我的截图
那么,有人可以帮忙吗?
更新苹果Retina屏幕Bug原因 ⚠️
苹果Retina屏幕
默认
自动缩放至1680px x 1050px
由于无法获取真正的视网膜屏幕大小比例,因此结果不会是2880px x 1800px
;
但下面的解决方案也是正确的,因为读取的屏幕尺寸为1680px x 1050px
,因此结果为3360px x 2100px
;
(function getResolution() {
const realWidth = window.screen.width * window.devicePixelRatio;
const realHeight = window.screen.height * window.devicePixelRatio;
console.log(`
Your screen resolution is: ${realWidth} x ${realHeight}
Your screen devicePixelRatio is: ${window.devicePixelRatio}
Your screen width is: ${window.screen.width}
Your screen height is: ${window.screen.height}
`);
})();
// Your screen resolution is: 3840 x 2160 (4K)
// Your screen resolution is: 3360 x 2100 ( 3K? Retina Screen)
// Your screen resolution is: 1920 x 1080 ( 1080P / FHD)
refs
https://www.cnblogs.com/xgqfrms/p/14196834.html
screen.width * window.devicePixelRatio
和screen.height * window.devicePixelRatio
,获取真实的屏幕分辨率。 - xgqfrms