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

前端 未结 24 1979
终归单人心
终归单人心 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

    A simple solution in jQuery to detect mouse usage, which solves the issue where mobile devices also trigger 'mousemove' event. Simply add a touchstart listener to remove the mousemove listener, so that it doesn't get triggered on touch.

    $('body').one('touchstart.test', function(e) {
      // Remove the mousemove listener for touch devices
      $('body').off('mousemove.test');
    }).one('mousemove.test', function(e) {
      // MOUSE!
    });
    

    Of course, the device could still be touch AND mouse, but the above will guarantee that a real mouse was used.

提交回复
热议问题