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

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

    We tried the modernizr implementation, but detecting the touch events is not consistent anymore (IE 10 has touch events on windows desktop, IE 11 works, because the've dropped touch events and added pointer api).

    So we decided to optimize the website as a touch site as long as we don't know what input type the user has. This is more reliable than any other solution.

    Our researches say, that most desktop users move with their mouse over the screen before they click, so we can detect them and change the behaviour before they are able to click or hover anything.

    This is a simplified version of our code:

    var isTouch = true;
    window.addEventListener('mousemove', function mouseMoveDetector() {
        isTouch = false;
        window.removeEventListener('mousemove', mouseMoveDetector);
    });
    

提交回复
热议问题