我正在开发一个网页应用程序(不是一个有很多有趣的文本页面的网站),它有一个非常不同的触摸界面(点击时你的手指会隐藏屏幕)和鼠标(很大程度上依赖于悬停预览)。我怎样才能检测到我的用户没有鼠标来显示正确的界面呢?我打算给同时有鼠标和触摸屏的人留一个开关(就像一些笔记本电脑一样)。
浏览器中的触摸事件功能实际上并不意味着用户正在使用触摸设备(例如,Modernizr不支持它)。如果设备有鼠标,则正确回答问题的代码应返回false,否则返回true。对于带有鼠标和触摸屏的设备,它应该返回false (不仅仅是触摸屏)
顺便说一句,我的触摸界面可能也适用于只有键盘的设备,所以我想要检测的更多的是缺少鼠标。
为了更清楚地说明这一需求,下面是我希望实现的API:
// Level 1
// The current answers provide a way to do that.
hasTouch();
// Returns true if a mouse is expected.
// Note: as explained by the OP, this is not !hasTouch()
// I don't think we have this in the answers already, that why I offer a bounty
hasMouse();
// Level 2 (I don't think it's possible, but maybe I'm wrong, so why not asking)
// callback is called when the result of "hasTouch()" changes.
listenHasTouchChanges(callback);
// callback is called when the result of "hasMouse()" changes.
listenHasMouseChanges(callback);
https://stackoverflow.com/questions/7838680
复制相似问题