一行代码判断是否移动端
怎么用一行代码判断是否移动端?ontouchend、userAgent。
ontouchend
通过判断是否支持触摸,支持触摸的设备大都都是移动端。
1 2 3 | if ('ontouchend' in document.body) { // 移动端 } |
这里为什么用ontouchend的原因是,ontouchend相对于ontouchstart、ontouchmove、ontouchcancel来说长度最短。
userAgent
通过判断用户代理字符串中是否包含mobile关键字。
1 2 3 | if (/mobile/i.test(navigator.userAgent)) { // 移动端 } |
来源:一行代码判断是否移动端
2019年5月5日 下午8:26
好文章!666,学习了