移動端橫屏的JS代碼(beta)

字號:


    這篇文章主要介紹了移動端橫屏的JS代碼(beta) 的相關(guān)資料,需要的朋友可以參考下
    直接給大家貼代碼了
    具體代碼如下所示:
    代碼如下:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
    <script>
    var orientation=0;
    function screenOrientationEvent(){
    if(orientation == 0)
    {
    document.getElementById("change").value="豎";
    $("body").css({
    'transform':'rotate(90deg)',
    '-webkit-transform':'rotate(90deg)',
    '-moz-transform':'rotate(90deg)'
    });
    }
    else
    {
    document.getElementById("change").value="橫";
    $("body").css({
    'transform':'rotate(0deg)',
    '-webkit-transform':'rotate(0deg)',
    '-moz-transform':'rotate(0deg)'
    });
    }
    }
    var innerWidthTmp = window.innerWidth;
    //橫豎屏事件監(jiān)聽方法
    function screenOrientationListener(){
    try{
    var iw = window.innerWidth;
    //屏幕方向改變處理
    if(iw != innerWidthTmp){
    if(iw>window.innerHeight)
    {
    orientation = 90;
    }
    else
    {
    orientation = 0;
    }
    //調(diào)用轉(zhuǎn)屏事件
    screenOrientationEvent();
    innerWidthTmp = iw;
    }
    }
    catch(e){alert(e);};
    //間隔固定事件檢查是否轉(zhuǎn)屏,默認500毫秒
    setTimeout("screenOrientationListener()",500);
    }
    //啟動橫豎屏事件監(jiān)聽
    screenOrientationListener();
    </script>
    <body onload="screenOrientationEvent()">
    <input id="change" type="text" value=""/>
    </body>
    </html>
    以上js代碼是實現(xiàn)移動端橫屏的核心代碼,代碼簡單易懂,所有沒給大家附太多的注釋