【发布时间】:2022-03-19 23:15:37
【问题描述】:
我目前使用以下测试(取自 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;
}
但有些设备是触摸和鼠标驱动的,所以我想要一个单独的函数来检测设备是否支持鼠标。进行此检查的好方法是什么?
最终我的意图是能够做到这些:
if(is_touch_device())
if(has_mouse_support())
if(is_touch_device() && has_mouse_support())
【问题讨论】:
-
虽然它没有明确回答您的问题,但我发现这篇文章很有趣,涉及触摸与鼠标支持:html5rocks.com/en/mobile/touchandmouse
-
未经测试(仅当鼠标实际移动时才有效): var mouse= false; window.onmousemove = function(){mouse= true}
-
@Reeno Clever,我认为这应该可行,但如果有人有已知的解决方案,将等待更经过测试的答案。编辑:这是真的,如果鼠标从不移动嗯,它会给出误报。
-
如果有人从不摆动鼠标,他们可能更喜欢其他交互方式......
-
嗯。根据html5rocks.com/en/mobile/touchandmouse/#toc-1 我的解决方案不起作用。该死:)关于这个话题的讨论很长:github.com/Modernizr/Modernizr/issues/869似乎没有一个好的解决方案
标签: javascript jquery