2

判断是否是微信

//判断是否是微信浏览器的函数,在微信登陆的地方需要
function isWeiXin() {
    //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息,这个属性可以用来判断浏览器类型
    var ua = window.navigator.userAgent.toLowerCase();
    //通过正则表达式匹配ua中是否含有MicroMessenger字符串
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

判断是否是ios

function isIOS() {
    var u = navigator.userAgent;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);     //ios终端
    return isiOS;
}

判断是否是pc端

/*检测设备,ISPC true  pc;false wx*/
function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
        "SymbianOS", "Windows Phone",
        "iPad", "iPod"
    ];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

var ISPC = IsPC();

项目里要求是否是pc/wx
微信分电脑微信和手机微信(android/ios)
手机端上进行横竖屏处理

if(ISPC){

}else if(isWeiXin()){
           if(window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {   
           //手机端
           if(isIOS()){
           //ios
            if (document.body.offsetWidth > document.body.offsetHeight) {
            //横屏
            } else if (document.body.offsetWidth < document.body.offsetHeight) {
            //竖屏
            }
           }else{
           //android
           if (screen && screen.orientation) {
                //手机微信andriod
                if (screen.orientation.type === 'landscape-primary') {
                //横屏
                 } else if (screen.orientation.type === 'portrait-primary') {
                 //竖屏
                 }
           }
         }else{
         //电脑端
         }
}

陌然浅笑
25 声望3 粉丝

坚持自律