如何通过js判断手机型号 iphone5 iphone5s iphone6 iphone7等等

原文:https://segmentfault.com/q/1010000002507636

纯网页开发的话用一下这个方法,我只是简单的用来根据不同的手机型号来做一些css样式的修改了。

 

Navigator 对象包含有关浏览器的信息

appVersion 返回浏览器的平台和版本信息

userAgent 返回由客户机发送服务器的user-agent头部的值

.indexOf()方法用来检测字符串值在字符串中首次出现的位置

    var events = navigator.userAgent;

    console.log(navigator.userAgent);
    console.log(navigator.appVersion);
    console.log(navigator)
    if(events.indexOf('Android')>-1 || events.indexOf('Linux')>-1 || events.indexOf('Adr')>-1){
        console.log("安卓手机");
    }else if(events.indexOf('iPhone')>-1){
        //根据尺寸进行判断 苹果的型号
        if(screen.height == 812 && screen.width == 375){
            console.log("苹果X");
        }else if(screen.height == 736 && screen.width == 414){
            console.log("iPhone7P - iPhone8P - iPhone6");
        }else if(screen.height == 667 && screen.width == 375){
            console.log("iPhone7 - iPhone8 - iPhone6");
        }else if(screen.height == 568 && screen.width == 320){
            console.log("iPhone5");
        }else{
            console.log("iPhone4");
        }
    }else if(events.indexOf('Windows Phone')>-1){
        console.log("诺基亚手机");
        
    }else if(events.indexOf("iPad")>-1){
        console.log("平板");
    }

或者可以用 window.screen.width 通过浏览器检测屏幕的宽度来判断这个值属于哪个手机尺寸范围

posted @ 2019-11-20 14:40  塞巴斯酱  阅读(2555)  评论(0编辑  收藏  举报