How to check browser for touchstart support using JS/jQuery?

前端 未结 4 1281
一生所求
一生所求 2020-11-28 05:12

In an attempt to follow best practices, we\'re trying to use the proper JavaScript/jQuery events according to what device you are using. For example, we\'re building a mobil

相关标签:
4条回答
  • 2020-11-28 05:44

    You can detect if the event is supported by:

    if ('ontouchstart' in document.documentElement) {
      //...
    }
    

    Give a look to this article:

    • Detecting event support without browser sniffing

    The isEventSupported function published there, is really good at detecting a wide variety of events, and it's cross-browser.

    0 讨论(0)
  • 2020-11-28 05:44

    You could check if typeof document.body.ontouchstart == "undefined" to fall back to normal dom events

    0 讨论(0)
  • 2020-11-28 05:48

    I made a full demostration that works in every browser with the full source code of the solution of this problem: Detect touch screen devices in Javascript.

    0 讨论(0)
  • 2020-11-28 05:55

    Use this code to detect if the device supports touch.

    Also work for windows 8 IE10 which uses 'MSPointerDown' event instead of 'touchmove'

    var supportsTouch = false;
    if ('ontouchstart' in window) {
        //iOS & android
        supportsTouch = true;
    
    } else if(window.navigator.msPointerEnabled) {
    
        // Windows
        // To test for touch capable hardware 
        if(navigator.msMaxTouchPoints) {
            supportsTouch = true;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题