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

后端 未结 30 2464
花落未央
花落未央 2020-11-21 23:50

I\'ve written a jQuery plug-in that\'s for use on both desktop and mobile devices. I wondered if there is a way with JavaScript to detect if the device has touch screen capa

相关标签:
30条回答
  • 2020-11-22 00:30

    This seems to be working fine for me so far:

    //Checks if a touch screen
    is_touch_screen = 'ontouchstart' in document.documentElement;
    
    if (is_touch_screen) {
      // Do something if a touch screen
    }
    else {
      // Not a touch screen (i.e. desktop)
    }
    
    0 讨论(0)
  • 2020-11-22 00:31

    I think the best method is:

    var isTouchDevice =
        (('ontouchstart' in window) ||
        (navigator.maxTouchPoints > 0) ||
        (navigator.msMaxTouchPoints > 0));
    if(!isTouchDevice){
        /* Code for touch device /*
    }else{
        /* Code for non touch device */
    }
    
    0 讨论(0)
  • 2020-11-22 00:33

    I would avoid using screen width to determine if a device is a touch device. There are touch screens much larger than 699px, think of Windows 8. Navigatior.userAgent may be nice to override false postives.

    I would recommend checking out this issue on Modernizr.

    Are you wanting to test if the device supports touch events or is a touch device. Unfortunately, that's not the same thing.

    0 讨论(0)
  • Update: Please read blmstr's answer below before pulling a whole feature detection library into your project. Detecting actual touch support is more complex, and Modernizr only covers a basic use case.

    Modernizr is a great, lightweight way to do all kinds of feature detection on any site.

    It simply adds classes to the html element for each feature.

    You can then target those features easily in CSS and JS. For example:

    html.touch div {
        width: 480px;
    }
    
    html.no-touch div {
        width: auto;
    }
    

    And Javascript (jQuery example):

    $('html.touch #popup').hide();
    
    0 讨论(0)
  • 2020-11-22 00:37

    Check out this post, it gives a really nice code snippet for what to do when touch devices are detected or what to do if touchstart event is called:

    $(function(){
      if(window.Touch) {
        touch_detect.auto_detected();
      } else {
        document.ontouchstart = touch_detect.surface;
      }
    }); // End loaded jQuery
    var touch_detect = {
      auto_detected: function(event){
        /* add everything you want to do onLoad here (eg. activating hover controls) */
        alert('this was auto detected');
        activateTouchArea();
      },
      surface: function(event){
        /* add everything you want to do ontouchstart here (eg. drag & drop) - you can fire this in both places */
        alert('this was detected by touching');
        activateTouchArea();
      }
    }; // touch_detect
    function activateTouchArea(){
      /* make sure our screen doesn't scroll when we move the "touchable area" */
      var element = document.getElementById('element_id');
      element.addEventListener("touchstart", touchStart, false);
    }
    function touchStart(event) {
      /* modularize preventing the default behavior so we can use it again */
      event.preventDefault();
    }
    
    0 讨论(0)
  • 2020-11-22 00:37

    I use:

    if(jQuery.support.touch){
        alert('Touch enabled');
    }
    

    in jQuery mobile 1.0.1

    0 讨论(0)
提交回复
热议问题