您当前的位置:首页 > 计算机 > 编程开发 > Html+Div+Css(前端)

一行代码判断是否移动端

时间:12-14来源:作者:点击数:
城东书院 www.cdsy.xyz

ontouchend

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

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

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

userAgent

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

if (/mobile/i.test(navigator.userAgent)) {
	// 移动端
}
城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐