真心感觉 H5 的强大啊,也感觉到一股热潮开始翻滚起来了,最近业界关于 H5 的报道争先恐后,貌似谁还不准备好的话就铁定吃亏了似的,就好像前几年 iOS、Android 刚兴起一样,抓住了机遇学到了技术要点,对自己以后的职业生涯发展会有很不一样的结果。
该功能是在 HTML5研究小组 看到的,然后自己在 iphone 上实践了下,还真是那么一回事呀。
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
|
var
SHAKE_THRESHOLD = 5000;
var
last_update = 0;
var
x, y, z, last_x = 0, last_y = 0, last_z = 0;
function
deviceMotionHandler(eventData) {
var
acceleration =eventData.accelerationIncludingGravity;
var
curTime =
new
Date().getTime();
if
((curTime-last_update)> 10) {
var
diffTime = curTime -last_update;
last_update = curTime;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var
speed = Math.abs(x +y + z - last_x - last_y - last_z) / diffTime * 10000;
if
(speed > SHAKE_THRESHOLD) {
alert(
"shake!"
);
}
last_x = x;
last_y = y;
last_z = z;
}
}
if
(window.DeviceMotionEvent) {
window.addEventListener(
'devicemotion'
,deviceMotionHandler,
false
);
}
else
{
document.getElementById(
"dmEvent"
).innerHTML =
"Not supported on your device."
}
|