How to detect if a device has mouse support?

后端 未结 6 1260
猫巷女王i
猫巷女王i 2020-12-03 20:45

I currently use the following test (taken out of Modernizr) to detect touch support:

function is_touch_device() {
    var bool;
    if((\'ontouchstart\' in w         


        
6条回答
  •  盖世英雄少女心
    2020-12-03 21:25

    There's a CSS media just for that!

    You can check whether some device has a mouse by getting the value of the pointer CSS media feature:

    if (matchMedia('(pointer:fine)').matches) {
      // Device has a mouse
    }
    

    Because it's CSS you don't even need to use JavaScript:

    @media (pointer: fine) {
      /* Rules for devices with mouse here */
    }
    

提交回复
热议问题