前端H5开发中的一些系统判断

1,976 阅读1分钟

1. 判断ios和安卓系统

  isSystem(e) {
    let u = navigator.userAgent
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 //  android终端
    if (isAndroid) {
      return 'android'
    }
    return 'ios'
  },

2. 判断微信浏览器类型

  checkPlatform() {
    if (/MicroMessenger/i.test(navigator.userAgent)) {
      // 这是微信平台下浏览器
      return 'Messenger'
    }
    if (/android/i.test(navigator.userAgent)) {
      // 这是Android平台下浏览器
      return 'Android'
    }
    if (/iphone os/i.test(navigator.userAgent)) {
      // 这是iOS平台下浏览器
      return 'iOS'
    }
    if (/Linux/i.test(navigator.userAgent)) {
      // 这是Linux平台下浏览器
      return 'Linux'
    }
    if (/Linux/i.test(navigator.platform)) {
      // 这是Linux操作系统平台
      return 'Linux'
    }
  },

3. 判断iphone底部黑线类型手机 iPhoneX,iPhone XS Max,iPhoneXR,iphone11

  isIphoneX() {
    // iPhone X、iPhone XS
    const isIPhoneX = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 375 && window.screen.height === 812
    // iPhone XS Max iphone11 Pro
    const isIPhoneXSMax = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896
    // iPhone XR iphone11
    const isIPhoneXR = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896
    if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) return true
    return false
  },