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

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

    You can install modernizer and use a simple touch event. This is very effective and works on every device I have tested it on including windows surface!

    I've created a jsFiddle

    function isTouchDevice(){
        if(Modernizr.hasEvent('touchstart') || navigator.userAgent.search(/Touch/i) != -1){
             alert("is touch");
                return true;
             }else{
                alert("is not touch");
                return false;
        }
    }
    

提交回复
热议问题