移动、PC、iphone、Andriod是否横屏【实用】

判断是否是PC端
if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    //移动端
}else{
    //PC
}
判断是否横屏
function orient() {
    console.log($(window).width())
    $('.back-box').css('display','none')
    if (window.orientation == 90 || window.orientation == -90) {
        //ipad、iphone竖屏;Andriod横屏
 $("body").attr("class", "landscape");
        orientation = 'landscape';
        return false;
    }
    else if (window.orientation == 0 || window.orientation == 180) {
        //ipad、iphone横屏;Andriod竖屏
 if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
            //移动端
            
        }else{
            //PC
        }
        $("body").attr("class", "portrait");
        orientation = 'portrait';
        return false;
    }
}
//页面加载时调用
$(function(){
    orient();
});
//用户变化屏幕方向时调用
$(window).bind( 'orientationchange', function(e){
    orient();
});

你可能感兴趣的:(javascript)