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

后端 未结 30 2472
花落未央
花落未央 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:21

    It looks like Chrome 24 now support touch events, probably for Windows 8. So the code posted here no longer works. Instead of trying to detect if touch is supported by the browser, I'm now binding both touch and click events and making sure only one is called:

    myCustomBind = function(controlName, callback) {
    
      $(controlName).bind('touchend click', function(e) {
        e.stopPropagation();
        e.preventDefault();
    
        callback.call();
      });
    };
    

    And then calling it:

    myCustomBind('#mnuRealtime', function () { ... });
    

    Hope this helps !

提交回复
热议问题