微信小程序识别机型屏幕比例

微信小程序识别机型屏幕比例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
handleScreenRatio () {
// 获取系统信息
const system = wx.getSystemInfoSync();

const width = system.screenWidth * system.devicePixelRatio;
const height = system.screenHeight * system.devicePixelRatio;

// 常见的比例,需自己添加更多
const list = [
[20, 9],
[19.5, 9],
[18, 9],
[16, 10],
[16, 9],
];

// 分隔符
const seperator = ':';

for(let i in list) {
let item = list[i];

// 先计算出屏幕大小除当前比例,结果的的绝对值会不会在我们的容错值范围内。
if(Math.abs(~~(height / item[0]) - ~~(width / item[1])) < 3) {
return item.join(seperator);
}
}

// wx.getRealtimeLogManager().info('未知屏幕比例', system);

return '未知';
},
往上