I'm developing a webapp (not a website with pages of interesting text) with a very different interface for touch (your finger hides the screen when you click) and mouse (relies heavily on hover preview).(我正在开发一个Web应用程序(不是一个包含有趣文本页面的网站),其触摸(鼠标单击时隐藏了屏幕)和鼠标(很大程度上取决于悬停预览)的界面非常不同。)
How can I detect that my user has no mouse to present him the right interface?(如何检测到我的用户没有鼠标向他展示正确的界面?) I plan to leave a switch for people with both mouse and touch (like some notebooks).(我计划为使用鼠标和触摸的人(例如某些笔记本电脑)留下一个开关。)
The touch event capability in the browser doesn't actually mean the user is using a touch device (for example, Modernizr doesn't cut it).(浏览器中的触摸事件功能实际上并不意味着用户正在使用触摸设备(例如,Modernizr不会对其进行裁剪)。) The code that correctly answers the question should return false if the device has a mouse, true otherwise.(如果设备有鼠标,则正确回答该问题的代码应返回false,否则返回true。) For devices with mouse and touch, it should return false (not touch only)(对于具有鼠标和触摸功能的设备,它应返回false(不仅限于触摸))
As a side note, my touch interface might also be suitable for keyboard-only devices, so it's more the lack of mouse I'm looking to detect.(附带说明一下,我的触摸界面也可能适用于仅键盘的设备,因此更多是我想检测不到的鼠标。)
To make the need more clear, here is the API that I'm looking to implement:(为了使需求更加清楚,这是我要实现的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);
ask by nraynaud translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…