-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwx.html
124 lines (121 loc) · 2.91 KB
/
wx.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv=X-UA-Compatible content='IE=edge,chrome=1'>
<meta name='renderer' content='webkit'>
<meta name='viewport' content='width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no'>
<title>横竖屏检测</title>
<style type="text/css">
/*landscape 是代表横屏,portrait代表竖屏*/
@media all and (orientation : landscape) {
.csslandscape{
display: block;
}
.cssportrait{
display: none;
}
}
@media all and (orientation : portrait) {
.csslandscape{
display: none;
}
.cssportrait{
display: block;
}
}
.portrait .jsportrait{
display: block;
}
.portrait .jslandscape{
display: none;
}
.landscape .jslandscape{
display: block;
}
.landscape .jsportrait{
display: none;
}
input{
border: 1px solid #ddd;
height: 26px;
}
div{
width: 180px;
height: 100px;
margin: 15px;
font-size: 20px;
}
</style>
</head>
<body>
<div>
<input type="text">
</div>
<div class="jslandscape">
js检测为横屏
</div>
<div class="jsportrait">
js检测为竖屏
</div>
<div class="csslandscape">
css检测为横屏
</div>
<div class="cssportrait">
css检测为竖屏
</div>
<div id="stat">
</div>
<div>
<input type="text">
</div>
<div>
<input type="text">
</div>
<script type="text/javascript">
(function(w)
{
var supportOrientation = (typeof w.orientation === 'number' && typeof w.onorientationchange === 'object');
w.supportOrientation=supportOrientation;
var init = function()
{
var htmlNode = document.body.parentNode, orientation;
var updateOrientation = function()
{
if(supportOrientation)
{
orientation = w.orientation;
switch(orientation)
{
case 90:
case -90:
orientation = 'landscape';
break;
default:
orientation = 'portrait';
break;
}
}
else
{
orientation = (w.innerWidth > w.innerHeight) ? 'landscape' : 'portrait';
}
htmlNode.setAttribute('class',orientation);
};
if(supportOrientation)
{
w.addEventListener('orientationchange',updateOrientation,false);
}
else
{
//监听resize事件
w.addEventListener('resize',updateOrientation,false);
}
updateOrientation();
};
w.addEventListener('DOMContentLoaded',init,false);
})(window);
document.getElementById('stat').innerHTML=window.supportOrientation?'支持js原生横竖屏判断':'不支持js原生横竖屏判断,js判断回滚到根据宽高比判断';
</script>
</body>
</html>