2 myq 26 myq_26 于 2013.02.26 10:56 提问

如何加载android 方向检测这个动能呢?

下面的代码用来在 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' />"));
        }
    });

2个回答

CSDN_Ask
CSDN_Ask   2013.02.27 13:40
已采纳

是条件的问题:

if(window.orientation == 90)
with the double =

使用 $('link[title=android]').get(0) 代替 $('link[title=android]')[0]

sunny_desmond
sunny_desmond   2017.09.14 13:47

默默的挖个坟,水经验。。。。。。

Csdn user default icon
上传中...
上传图片
插入图片