我目前使用以下测试(取自Modernizr)来检测触摸支持:
function is_touch_device() {
var bool;
if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
bool = true;
} else {
injectElementWithStyles(['@media (',prefixes.join('touch-enabled),('),mod,')','{#modernizr{top:9px;position:absolute}}'].join(''), function(node) {
bool = node.offsetTop === 9;
});
}
return bool;
}
Run Code Online (Sandbox Code Playgroud)
但有些设备是触摸和鼠标驱动的,所以我想要一个单独的功能来检测设备是否有鼠标支持.有什么好办法做这个检查?
最终我的意图是能够做到这些:
if(is_touch_device())
if(has_mouse_support())
if(is_touch_device() && has_mouse_support())
Run Code Online (Sandbox Code Playgroud)