Detecting that the browser has no mouse and is touch-only

前端 未结 24 2036
终归单人心
终归单人心 2020-11-27 09:35

I\'m developing a webapp (not a website with pages of interesting text) with a very different interface for touch (your finger hides the screen when you click) and mouse (re

24条回答
  •  盖世英雄少女心
    2020-11-27 10:00

    I don't think it's possible to identify touch-only device (to my knowledge of course). The main issue is all mouse and keyboard events are fired by touch devices too. See the following example, both alerts return true for touch devices.

    function is_touch_present() {
      return ('ontouchstart' in window) || ('onmsgesturechange' in window);
    }
    
    function is_mouse_present() {
      return (('onmousedown' in window) && ('onmouseup' in window) && ('onmousemove' in window) && ('onclick' in window) && ('ondblclick' in window) && ('onmousemove' in window) && ('onmouseover' in window) && ('onmouseout' in window) && ('oncontextmenu' in window));
    }
    
    alert("Touch Present: " + is_touch_present());
    alert("Mouse Present: " + is_mouse_present());
    

提交回复
热议问题