过去,检查鼠标是否存在的最佳方法是寻找触摸事件支持。但是,桌面 Chrome 现在支持触摸事件,这使得该测试失败。
有没有办法直接测试鼠标悬停事件支持,而不是根据触摸事件的存在来推断它?
解决方案:根据 AshleysBrain 的回答,这是有效的代码。
jQuery(function()
{
// Has mouse
jQuery("body").one("mousemove", function(e)
{
attachMouseEvents();
});
// Has touchscreen
jQuery("body").one("touchstart", function(e)
{
// Unbind the mouse detector, as this will fire on some touch devices. Touchstart should always fire first.
jQuery("body").unbind("mousemove");
attachTouchEvents();
});
});