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

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

    Update: Please read blmstr's answer below before pulling a whole feature detection library into your project. Detecting actual touch support is more complex, and Modernizr only covers a basic use case.

    Modernizr is a great, lightweight way to do all kinds of feature detection on any site.

    It simply adds classes to the html element for each feature.

    You can then target those features easily in CSS and JS. For example:

    html.touch div {
        width: 480px;
    }
    
    html.no-touch div {
        width: auto;
    }
    

    And Javascript (jQuery example):

    $('html.touch #popup').hide();
    

提交回复
热议问题