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

前端 未结 24 2041
终归单人心
终归单人心 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 09:51

    How about listening for a mousemove event on the document. Then until you hear that event you assume that the device is touch or keyboard only.

    var mouseDetected = false;
    function onMouseMove(e) {
      unlisten('mousemove', onMouseMove, false);
      mouseDetected = true;
      // initializeMouseBehavior();
    }
    listen('mousemove', onMouseMove, false);
    

    (Where listen and unlisten delegate to addEventListener or attachEvent as appropriate.)

    Hopefully this wouldn't lead to too much visual jank, it would suck if you need massive re-layouts based on mode...

提交回复
热议问题