Skip to content

isMobile

检测当前设备是否为移动端(手机或平板)。

函数签名

typescript
function isMobile(deps: IsMobileDeps): boolean

interface IsMobileDeps {
  userAgent: string
  hasTouch: boolean
  innerWidth: number
}

参数

参数名类型必填说明
depsIsMobileDeps依赖对象
deps.userAgentstring用户代理字符串
deps.hasTouchboolean是否支持触摸屏
deps.innerWidthnumber屏幕宽度(像素)

返回值

类型说明
booleantrue 表示移动端设备,false 表示桌面设备

工作原理

  1. User Agent 检测: 匹配常见移动设备关键词(Android、iPhone、iPad 等)
  2. 触摸屏检测: 检查是否支持触摸事件
  3. 屏幕宽度检测: 检查屏幕宽度是否小于等于 768px

判断逻辑:

User Agent 匹配 OR (触摸屏支持 AND 屏幕宽度 ≤ 768px)

检测的移动设备包括:

  • Android 设备
  • iPhone
  • iPad
  • iPod
  • BlackBerry
  • Windows Phone (IEMobile)
  • Opera Mini

异常

无异常抛出