How to determine if the client is a touch device

This question already has an answer here:

  • What's the best way to detect a 'touch screen' device using JavaScript? 34 answers

  • You can use the following JS function:

    function isTouchDevice() {
       var el = document.createElement('div');
       el.setAttribute('ongesturestart', 'return;'); // or try "ontouchstart"
       return typeof el.ongesturestart === "function";
    }
    

    Source: Detecting touch-based browsing.

    Please note the above code only tests if the browser has support for touch, not the device.

    Related links:

  • Optimize website for touch devices
  • What is the best way to detect a mobile device in jQuery?
  • What's the best way to detect a 'touch screen' device using JavaScript?
  • Mozilla.org Detecting touch: it's the 'why', not the 'how'
  • There may be detection in jquery for mobile and jtouch


    if ("ontouchstart" in document.documentElement)
    {
      alert("It's a touch screen device.");
    }
    else
    {
     alert("Others devices");
    }
    

    在浏览大量代码后,我发现了最简单的代码


    Include modernizer, which is a tiny feature detection library. Then you can use something like below.

    if (Modernizr.touch){
       // bind to touchstart, touchmove, etc and watch `event.streamId`
    } else {
       // bind to normal click, mousemove, etc
    }
    
    链接地址: http://www.djcxy.com/p/14726.html

    上一篇: 在响应式网站上检测移动设备的最大视频分辨率支持

    下一篇: 如何确定客户端是否是触摸设备