下面的代碼用來在 android 設備上檢測方向,在旋轉設備上加載樣式表時很好用,但是如果設備是橫向的(在JS中是90 / -90) ,下面的代碼就不對了。如何加載android 方向檢測這個動能呢?
//detect orientation change
var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
window.addEventListener(orientationEvent, function() {
if(window.orientation == 90) {
$('link[title=android]')[0].disabled=true;
$("head").append($("<link title='android_90' rel='stylesheet' href='css/android_90.css' type='text/css' />"));
} else if (window.oreintation == -90) {
$('link[title=android]')[0].disabled=true;
$("head").append($("<link title='android_90' rel='stylesheet' href='css/android_90.css' type='text/css' />"));
} else if (window.oreintation == 0){
$('link[title=android_90]')[0].disabled=true;
$("head").append($("<link title='android' rel='stylesheet' href='css/android.css' type='text/css' />"));
} else if (window.oreintation == 180){
$('link[title=android_90]')[0].disabled=true;
$("head").append($("<link title='android' rel='stylesheet' href='css/android.css' type='text/css' />"));
}
}, false);
//check on window.load
$(document).ready(function () {
if(window.orientation == 90) {
$('link[title=android]')[0].disabled=true;
$("head").append($("<link title='android_90' rel='stylesheet' href='css/android_90.css' type='text/css' />"));
} else if (window.oreintation == -90) {
$('link[title=android]')[0].disabled=true;
$("head").append($("<link title='android_90' rel='stylesheet' href='css/android_90.css' type='text/css' />"));
} else if (window.oreintation == 0){
$('link[title=android_90]')[0].disabled=true;
$("head").append($("<link title='android' rel='stylesheet' href='css/android.css' type='text/css' />"));
} else if (window.oreintation == 180){
$('link[title=android_90]')[0].disabled=true;
$("head").append($("<link title='android' rel='stylesheet' href='css/android.css' type='text/css' />"));
}
});
是條件的問題:
if(window.orientation == 90)
with the double =
使用 $('link[title=android]').get(0)
代替 $('link[title=android]')[0]