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

前端 未结 4 1303
一生所求
一生所求 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: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;
        }
    
    }
    

提交回复
热议问题