获取页面视口大小
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
var size = getViewPortSize()
console.log(size)
function getViewPortSize() {
var pageWidth = window.innerWidth,
pageHeight = window.innerHeight
if (typeof pageWidth != 'number') {
if (document.compatMode == 'CSS1Compat') {
// 是在标准模式下
pageWidth = document.documentElement.clientWidth
pageHeight = document.documentElement.clientHeight
} else {
// 混杂模式下
pageWidth = document.body.clientWidth
pageHeight = document.body.clientHeight
}
}
return {
pageWidth,
pageHeight,
}
}
</script>
</body>
</html>
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
33
34
35
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
33
34
35
编辑 (opens new window)
上次更新: 2021/08/21 17:31:26