当前位置: 首页 > news >正文

js判断当前设备是否为移动端

背景

响应式布局:随着移动互联网的发展,用户通过手机和平板访问网页的比例越来越高。为了提供更好的用户体验,开发者需要根据设备类型调整页面布局、字体大小、图片尺寸等,以确保在不同屏幕尺寸上都有良好的显示效果。
交互方式差异:移动设备和桌面设备的交互方式有很大不同(例如触控屏 vs 鼠标点击),因此需要针对不同的交互方式进行适配。

编码实战

/*** 判断当前设备是否为移动设备(包括手机、平板、微信小程序 WebView 等)* @returns {boolean} 是否为移动端*/
function isMobileDevice() {// 获取浏览器的 User-Agent 字符串,用于识别操作系统、浏览器类型等信息const ua = navigator.userAgent || navigator.vendor || window.opera;/*** 检测是否是常见的移动设备* 匹配关键词:Android、iPhone、iPad、iPod、BlackBerry、webOS、Windows Phone、SymbianOS、IEMobile、Opera Mini* 适用于大多数主流手机和平板设备*/const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|webOS|Windows Phone|SymbianOS|IEMobile|Opera Mini/i.test(ua);/*** 检测是否是平板设备(如 iPad、Kindle Fire、黑莓 PlayBook 等)* 特别处理了 Amazon Fire 平板的特征码 KFxxx* 注意:iPadOS 在桌面 Safari 中 UA 可能显示为 Macintosh,所以补充了 touch 关键词检测*/const isTablet = /ipad|playbook|silk|(android.*\bkf[a-z]{2,6}\b)|kindle|macintosh.*touch/i.test(ua);/*** 检测设备是否支持触控屏* 使用多种方式兼容不同浏览器:- 'ontouchstart' in window:标准触控事件是否存在- navigator.maxTouchPoints:多点触控支持数量(现代浏览器)- navigator.msMaxTouchPoints:IE 和旧版 Edge 兼容*/const hasTouchScreen = (('ontouchstart' in window) ||(navigator.maxTouchPoints > 0) ||(navigator.msMaxTouchPoints > 0));/*** 判断是否是 PC 端的微信环境(用于后续排除 PC 微信误判)* 如果 UA 中包含 Windows NT 或 Macintosh,则认为是桌面端微信*/const isPcInWeChat = /Windows NT|Macintosh/.test(ua);/*** 判断是否是微信内置浏览器或小程序 WebView* 但需要排除 PC 微信的情况(否则可能误判为移动端)*/const isWeChatWebView = /micromessenger/i.test(ua) && !isPcInWeChat;/*** 判断是否是从微信小程序加载的 Web 页面(可选)* 小程序加载 web-view 时可以通过 URL 参数传递标志位* 示例:<web-view src="https://yourdomain.com/page?from=miniprogram" />*/const isInMiniProgram = /from=miniprogram/.test(window.location.search);/*** 最终判断逻辑:* 满足以下任意条件即判定为“移动端”:** 1. 是常见移动设备(如 Android、iPhone)* 2. 是平板设备(如 iPad、Kindle)* 3. 支持触控屏,并且窗口宽度小于 1024px(模拟移动端体验)* 4. 是微信小程序 WebView(非 PC 环境)* 5. 是从微信小程序加载的页面(通过 URL 参数识别)*/return isMobile || isTablet || (hasTouchScreen && window.innerWidth < 1024) || isWeChatWebView || isInMiniProgram;
}

无注释版本

function isMobileDevice() {const ua = navigator.userAgent || navigator.vendor || window.opera;const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|webOS|Windows Phone|SymbianOS|IEMobile|Opera Mini/i.test(ua);const isTablet = /ipad|playbook|silk|(android.*\bkf[a-z]{2,6}\b)|kindle|macintosh.*touch/i.test(ua);const hasTouchScreen = (('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0));const isPcInWeChat = /Windows NT|Macintosh/.test(ua);const isWeChatWebView = /micromessenger/i.test(ua) && !isPcInWeChat;const isInMiniProgram = /from=miniprogram/.test(window.location.search); // 如果你控制了 URLreturn isMobile || isTablet || (hasTouchScreen && window.innerWidth < 1024) || isWeChatWebView || isInMiniProgram;
}
http://www.xdnf.cn/news/669547.html

相关文章:

  • camera_venc_thread线程获取高分辨率编码码流
  • Vue组件化
  • Rust 学习笔记:关于闭包的练习题
  • Flink系列文章列表
  • 分布式系统中的网络编程
  • wordpress迁移到Hostinger
  • 爬虫入门指南-某专利网站的专利数据查询并存储
  • YOLOv2 深度解析:目标检测领域的进阶之路
  • 【文献阅读】EndoChat: Grounded Multimodal Large Language Model for Endoscopic Surgery
  • 【HW系列】—目录扫描、口令爆破、远程RCE流量特征
  • 攻防世界-ics-07
  • 【Web应用】基础篇04-功能详解-权限控制(创建菜单--分配角色--创建用户)
  • 使用 scikit-learn 库对乌克兰冲突事件数据集进行多维度分类分析
  • ABP VNext + Apache Flink 实时流计算:打造高可用“交易风控”系统
  • 【深度学习】9. CNN性能提升-轻量化模型专辑:SqueezeNet / MobileNet / ShuffleNet / EfficientNet
  • 汽车电子/电气(E/E)架构将朝着区域(分区)式架构方向发展
  • Filebeat es 同步服务器日志到es
  • C++ STL 容器:List 深度解析与实践指南
  • Linux编辑器——vim的使用
  • 文件上传白名单绕过(图片马 - 图片二次渲染绕过)
  • React从基础入门到高级实战:React 核心技术 - React 与 TypeScript:构建类型安全的应用
  • 第十章:构建之巅 · 打包与部署的终极试炼
  • uniapp-商城-72-shop(5-商品列表,步进器添加商品到的购物车实现)
  • Unsupervised Learning-Word Embedding
  • 如何提高CAD作图设计效率,技术分享
  • 每日算法 -【Swift 算法】实现回文数判断!
  • stm32f系列工程切换到H系列
  • 电芯单节精密焊接机:以先进功能与特点赋能电池制造科技升级
  • 传统数据表设计与Prompt驱动设计的范式对比:以NBA投篮数据表为例
  • PHPStudy 一键式网站搭建工具的下载使用