1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 判断当前终端是移动端还是PC端

判断当前终端是移动端还是PC端

时间:2021-10-14 02:21:36

相关推荐

判断当前终端是移动端还是PC端

引入文件isPc.js

const browser = () => {// 获取浏览器UA标识var u = navigator.userAgent;console.log(u);// Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36return {trident: u.indexOf('Trident') > -1, // IE内核presto: u.indexOf('Presto') > -1, // opera内核webKit: u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') === -1, // 火狐内核mobile: !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, // android终端iPhone: u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器iPad: u.indexOf('iPad') > -1, // 是否iPadwebApp: u.indexOf('Safari') === -1, // 是否web应该程序,没有头部与底部weixin: u.indexOf('MicroMessenger') > -1, // 是否微信 (-01-22新增)qq: u.match(/\sQQ/i) === ' qq', // 是否QQ};};const isPc = () => {let isPcOrMobile = true;if (browser().mobile || browser().android || browser.ios || document.body.clientWidth < 787) {// 判断是否是移动端console.log('这里是移动端');isPcOrMobile = false;} else {console.log('这里是pc端');isPcOrMobile = true;}window.addEventListener('resize', () => {if (browser().mobile || browser().android || browser.ios || document.body.clientWidth < 787) {// 判断是否是移动端console.log('这里是移动端');isPcOrMobile = false;} else {console.log('这里是pc端');isPcOrMobile = true;}});return isPcOrMobile;};export default isPc;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。