通过判断是否支持触摸,支持触摸的设备大都都是移动端。
if ('ontouchend' in document.body) {
// 移动端
}
这里为什么用 ontouchend 的原因是,ontouchend 相对于 ontouchstart、ontouchmove、ontouchcancel 来说长度最短。
通过判断用户代理字符串中是否包含 mobile 关键字。
if (/mobile/i.test(navigator.userAgent)) {
// 移动端
}
