-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
191 lines (166 loc) · 4.9 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
list-style: none;
}
html{
background: #333;
color: #fff;
}
.box{
position: relative;
top: 100px;
margin: 0 auto;
width: 300px;
height: 300px;
/* border: 1px solid #fff; */
}
.pin{
position: absolute;
top: -50px;
left: 50%;
margin-left: -2px;
width: 4px;
height: 50px;
background: #fff;
}
.ten{
position: absolute;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -100px;
width: 200px;
height: 200px;
}
.h, .l{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #fff;
}
.ten .h{
width: 2px;
height: 200px;
}
.ten .l{
width: 200px;
height: 2px;
}
ul{
width: 300px;
height: 300px;
border-radius: 50%;
transition: all 10ms linear;
}
li{
position: absolute;
top: 0;
left: 50%;
margin-left: -1px;
width: 2px;
height: 20px;
background: red;
transform-origin: 1px 150px;
}
span{
position: absolute;
top: -20px;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
}
</style>
</head>
<body>
<p>方向:<div id="heading">0</div></p>
<div class="box">
<div class="pin"></div>
<div class="ten">
<div class="l"></div>
<div class="h"></div>
</div>
<ul>
<li><span style="color:red">0</span></li>
<li><span>330</span></li>
<li><span>300</span></li>
<li><span>270</span></li>
<li><span>240</span></li>
<li><span>210</span></li>
<li><span>180</span></li>
<li><span>150</span></li>
<li><span>120</span></li>
<li><span>90</span></li>
<li><span>60</span></li>
<li><span>30</span></li>
</ul>
</div>
<script>
function lineDraw() {
var li = document.getElementsByTagName('li');
var span = document.getElementsByTagName('span');
var len = li.length;
for(var i = 0; i < len; i++) {
// span[i].style.transform = 'rotate('+ -30 * i+'deg)';
li[i].style.transform = 'rotate('+30*i+'deg)';
}
}
lineDraw();
function move(ang) {
var ul = document.getElementsByTagName('ul')[0];
ul.style.transform= 'rotate('+ang+'deg)';
}
function DeviceOrientationHandler(event){
var webkitCompassHeading = event.webkitCompassHeading;
// if(webkitCompassHeading != null){
//判断屏幕方向
var head = '';
var headNum = Math.round(Math.round(webkitCompassHeading/45) + 7)%8;
switch (headNum) {
case 0:
head = '东北';
break;
case 1:
head = '东';
break;
case 2:
head = '东南';
break;
case 3:
head = '南';
break;
case 4:
head = '西南';
break;
case 5:
head = '西';
break;
case 6:
head = '西北';
break;
case 7:
head = '北';
}
move(webkitCompassHeading);
document.getElementById('heading').innerHTML = head + ' ' + Math.round(webkitCompassHeading);
// }else{
// document.body.innerHTML = "当前浏览器不支持DeviceOrientation";
// }
}
if(window.DeviceOrientationEvent){
window.addEventListener('deviceorientation',DeviceOrientationHandler, false);
}else{
alert("您的浏览器不支持DeviceOrientation");
}
</script>
</body>
</html>