如果有人正在寻找问题的 Webview/javascript 解决方案,下面可以做到这一点。
这将触发窗口上的自定义“翻转”事件,并带有 jquery 的“额外参数”。它还设置了 window.flip,类似于 window.orientation:
$(window).on('flip',function(ev,angle,orientation) {
console.log(angle,orientation);
alert(window.flip);
});
if (window.DeviceOrientationEvent) {
jQuery.flip = {
debug : false,
interval : 1000,
checked : false,
betaflat : 25,
gammaflat : 45,
orientation : 'portrait-primary',
angles : {
'portrait-primary' : 0,
'portrait-secondary' : 0,
'landscape-primary' : 90,
'landscape-secondary' : -90
},
timer : null,
check : function(ev) {
if (!this.checked) {
var trigger=false;
if (this.debug) console.log([ev.alpha,ev.beta,ev.gamma]);
if (ev.beta>this.betaflat) {
// if beta is big its portrait
if (this.debug) console.log('beta portrait pri');
if (this.orientation!='portrait-primary') {
this.orientation='portrait-primary';
trigger=true;
}
} else if (ev.beta<-this.betaflat) {
// if beta is big its portrait
if (this.debug) console.log('beta portrait sec');
if (this.orientation!='portrait-secondary') {
this.orientation='portrait-secondary';
trigger=true;
}
} else if (ev.gamma>this.gammaflat) {
// else if gamma is big its landscape
if (this.debug) console.log('gamma landscape pri');
if (this.orientation!='landscape-primary') {
this.orientation='landscape-primary';
trigger=true;
}
} else if (ev.gamma<-this.gammaflat) {
// else if gamma is big its landscape
if (this.debug) console.log('gamma landscape sec');
if (this.orientation!='landscape-secondary') {
this.orientation='landscape-secondary';
trigger=true;
}
}
if (trigger) {
if (this.debug) console.log('trigger flip');
window.flip = this.angles[this.orientation];
$(window).trigger('flip',[window.flip,this.orientation]);
this.checked=true;
}
}
}
}
$(document).ready(function() {
setInterval(function() {jQuery.flip.checked=false},jQuery.flip.interval);
$(window).on('deviceorientation',function(ev) { jQuery.flip.check(ev.originalEvent) });
});
} else {
if (this.debug) console.log('DeviceOrientationEvent not supported');
}
jquery 并不是真正需要的。无论如何我都需要它。