FED

©FrontEndDev.org
2015 - 2024
web@2.22.0 api@2.20.0

一行代码判断是否移动端

ontouchend

通过判断是否支持触摸,支持触摸的设备大都都是移动端。

if ('ontouchend' in document.body) {
	// 移动端
}

这里为什么用ontouchend的原因是,ontouchend相对于ontouchstartontouchmoveontouchcancel来说长度最短。

userAgent

通过判断用户代理字符串中是否包含mobile关键字。

if (/mobile/i.test(navigator.userAgent)) {
	// 移动端
}