检测设备方向
if(window.DeviceOrientationEvent){
window.addEventListener("deviceorientation",handleOrientation,true)
}else{
console.log("对不起,您的浏览器不支持Device Orientation");
}
function handleOrientation(orientData) {
var absolute = orientData.absolute;
var alpha = orientData.alpha;
var beta = orientData.beta;
var gamma = orientData.gamma;
}
if(window.DeviceMotionEvent){
window.addEventListener("devicemotion", handleMotion, true);
}
window.addEventListener("orientationchange", function() { // 获取新方向的数值 if(window.orientation==180||window.orientation==0){ alert("竖屏状态!") } if(window.orientation==90||window.orientation==-90){ alert("横屏状态!") } }, false);
window.addEventListener("resize", function(e) {
// 获取新方向的数值
orientChk = document.documentElement.clientWidth > document.documentElement.clientHeight?'landscape':'portrait';
if(orientChk =='lapdscape'){
//这里是横屏下需要执行的事件
}else{
//这里是竖屏下需要执行的事件
}
}, false);
/* 横屏 */
@media screen and (orientation:portrait) { /* portrait-specific styles */ } /* 竖屏*/ @media screen and (orientation:landscape) { /* landscape-specific styles */ }
var SHAKE_THRESHOLD = 3000;var last_update = 0;
var x = y = z = last_x = last_y = last_z = 0;
function deviceMotionHandler(eventData) {
var acceleration = eventData.accelerationIncludingGravity;
var curTime = new Date().getTime();
var diffTime = curTime - last_update;
if (diffTime > 100) {
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) {
//你被摇动了
}
//记录上一次加速度
last_x = x;
last_y = y;
last_z = z;
}
}
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
}