如何判断当前环境是移动端还是PC端 #119
Anuluca Date : 2021-03-05 Tags : 2
方法1:navigator.userAgent
函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| isPc() { var userAgentInfo = navigator.userAgent var Agents = new Array( 'Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod' ) var flag = true flag = !Agents.some((ele) => { return userAgentInfo.indexOf(ele) > 0 }) return flag }
|
对于 Android/iPhone 可以匹配以下正则:
1 2 3 4 5
| const appleIphone = /iPhone/i; const appleIpod = /iPod/i; const appleTablet = /iPad/i; const androidPhone = /\bAndroid(?:.+)Mobile\b/i; const androidTablet = /Android/i;
|
方法2:isMobile库
github链接
1 2 3
| import isMobile from 'ismobilejs'
const mobile = isMobile()
|