检测鼠标支持
这与检测浏览器中的触摸支持相反。如何检测浏览器是否支持鼠标?桌面版 Chrome 应返回 true,iPad 版 Safari 应返回 false。
我认为移动浏览器对于通常的检测技巧会返回误报。
This is the opposite of detecting touch support in browsers. How can I detect if a browser has mouse support? Chrome for desktop should return true, Safari for iPad should return false.
I'm thinking that mobile browsers will return false positives for the usual detection tricks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在使用触摸事件的浏览器中:
var clickEvent = ('ontouchstart' in window ? 'touchend' : 'click');
基本上是说“如果设备支持触摸,则只听触摸端而不是单击”——在多输入设备上,这会立即阻止通过鼠标、触控板或键盘进行的任何交互。
本文此处详细讨论了您的问题
另一篇富有洞察力的文章此处
但是,这完全取决于您想要什么达到。
In browsers that use Touch Events:
var clickEvent = ('ontouchstart' in window ? 'touchend' : 'click');
is basically saying “if the device support touch, only listen to touchend and not click” – which, on a multi-input device, immediately shuts out any interaction via mouse, trackpad or keyboard.
This article discusses your question in details here
Another insightful article here
But, it all depends on what you want to achieve.
理论上,您应该能够测试它是否支持触摸,然后进行相反的操作。我可能是错的,但据我所知,不支持触摸事件的浏览器将支持鼠标事件,反之亦然,因为这两者之间的区别通常是移动浏览器与常规浏览器。
编辑:另一种可能性可能是使用 onmousemove ,就像使用 ontouchmove 来检测触摸功能一样。但是,我不确定移动浏览器是否会注册 onmousemove。
Theoretically, you should be able to test if it has touch support, then take the reverse. I could be wrong, but as far as I know, browsers that don't support touch events will support mouse events, and vice versa, as the difference between these two is usually mobile vs. regular browsers.
EDIT: Another possibility might be using onmousemove in the same way you use ontouchmove to detect touch capability. However, I'm not sure if mobile browsers would register onmousemove.
此方法在不使用事件的情况下检查
Touch
对象是否存在。如果没有,你就有鼠标了。我认为这是最简单的方法。在具有触摸和鼠标功能的罕见设备上,它将返回false
,因此您必须确定这是否会影响您的设计。演示: http://jsfiddle.net/ThinkingStiff/ux89v/
脚本:
HTML:
This method checks if the
Touch
object exists without using events. If it doesn't, you have a mouse. I think that's the easiest way to do it. It will returnfalse
on the rare device that has touch and a mouse, so you'll have to decide if that affects your design or not.Demo: http://jsfiddle.net/ThinkingStiff/ux89v/
Script:
HTML: