ontouchend

通过判断是否支持触摸,支持触摸的设备大都都是移动端。
if ('ontouchend' in document.body) {
    // 移动端
}
  这里为什么用 ontouchend的原因是, ontouchend相对于 ontouchstartontouchmoveontouchcancel来说长度最短。

userAgent

通过判断用户代理字符串中是否包含 mobile关键字。
if (/mobile/i.test(navigator.userAgent)) {
    // 移动端
}