如何检测设备模式仿真? [英] How to detect device mode emulation?

查看:22
本文介绍了如何检测设备模式仿真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考 Chrome 模拟设备分辨率和触摸事件的能力:https://developer.chrome.com/devtools/docs/device-mode

Referring to Chrome's ability to emulate device resolution and touch events: https://developer.chrome.com/devtools/docs/device-mode

实际问题:

'ontouchstart' in document.documentElement

在设备仿真模式下返回 false(当它实际上能够执行触摸事件时).因此,我需要一个设备模式仿真的例外.

In device emulation mode returns false (when it is in fact capable of performing a touch event). Thus, I need an exception for the device mode emulation.

推荐答案

您需要在窗口中检查 ontouchstart.不是 document.documentElement.

You need to check for ontouchstart in window. Not document.documentElement.

要检测触摸事件:

'TouchEvent' in window && 'ontouchstart' in window

我应该提到我没有包括 Modernizr 使用的 window.DocumentTouch 方法.它仅适用于 Firefox <25 和 Mozilla 在 Windows 上放弃了 Touch Metro UI.所以我看不到任何合理或相关的浏览器使用来真正证明它的合理性.虽然 TouchEvent in window 不是必需的,但我认为检查 API 的存在也更具未来证明和技术上的正确性.

I should mention that I am not including the window.DocumentTouch method used by Modernizr. It's only for Firefox < 25 and Mozilla dropped the Touch Metro UI on Windows. So I don't see any plausible or relevant browser usage to really justify it any longer. And while TouchEvent in window isn't required, I think it's more future proof and technically correct to check the API's presence too.

有关触摸事件检测的更多详细信息,请参阅最新的 Modernizr Touch事件功能检测,带有链接和评论中的引用.

For more details on Touch Events detection see the latest Modernizr Touch Event Feature Detect with link and references in comments.

这篇关于如何检测设备模式仿真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆