$(window).bind("orientationchange", function(){
var orientation = window.orientation;
var new_orientation = (orientation) ? 0 : 180 + orientation;
$('body').css({
"-webkit-transform": "rotate(" + new_orientation + "deg)"
});
});
这是一种危险的方法,但认为这是唯一的方法。
您也可以绑定到窗口调整大小事件。
$(window).bind("resize", function(){
var orientation = window.orientation;
var new_orientation = (orientation) ? 0 : 180 + orientation;
$('body').css({
"-webkit-transform": "rotate(" + new_orientation + "deg)"
});
});
我不久前写了这个小脚本:它修复了 iOS safari 中的乘法调整大小回调错误和 android 中的非/延迟/早期触发(1)方向更改错误。
(1)有时在浏览器改变宽度+高度之前有时不触发?有线!
if (!(/iphone|ipad/gi).test(navigator.appVersion)) {
$(window).unbind("resize").bind("resize", function() {
$(window).trigger("orientationchange");
});
}
如果不是 iphone 或 ipad,您将触发窗口上的orientationchange 事件。
因此,当您绑定任何调整大小的函数时,它会抛出方向变化。
See: http://jsfiddle.net/aalouv/sABRQ/1/ http://jsfiddle.net/aalouv/sABRQ/1/