我正在开发一个webapp(不是一个有趣文本页面的网站),它有一个非常不同的触摸界面(当你点击时你的手指隐藏屏幕)和鼠标(严重依赖于悬停预览).如何检测到我的用户没有鼠标向他显示正确的界面?我计划为鼠标和触摸的人留下一个开关(就像一些笔记本电脑).
浏览器中的触摸事件功能实际上并不意味着用户正在使用触摸设备(例如,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()" …Run Code Online (Sandbox Code Playgroud) 有人能告诉我,如果我认为jQuery的$ .browser已被弃用了吗?
我现有的实现会继续有效吗?如果没有,是否有一个易于实现的替代方案.
我使用 JQuery 1.7 来处理事件。
function objToStr(obj) {
return ...string representation of the object
}
$slider.mousedown(function(e) {
console.log("$slider.mousedown:\n" + objToStr(e) );
});
$slider.mousemove( function(e) {
console.log("$slider.mousemove:\n" + objToStr(e) );
});
Run Code Online (Sandbox Code Playgroud)
这是结果:
$slider.mousedown:
altKey = false
attrChange = undefined
attrName = undefined
bubbles = true
button = 0
buttons = undefined
cancelable = true
clientX = 319
clientY = 13
ctrlKey = false
currentTarget = object
data = null
delegateTarget = object
eventPhase = 3
fromElement = undefined
handleObj = object
isDefaultPrevented …Run Code Online (Sandbox Code Playgroud)