How to detect device mode emulation?

前端 未结 3 769
广开言路
广开言路 2021-01-20 02:24

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

The actual issue:



        
3条回答
  •  执念已碎
    2021-01-20 03:12

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

    Touch Events are to be detected with:

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

    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.

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

提交回复
热议问题