Modernizr.touch在firefox浏览器上返回true

Sam*_*iri 10 javascript firefox touch-event modernizr

我写了一个代码的和平,以触摸和非触摸为基础的事件.它适用于所有其他浏览器和设备,但Firefox.默认FF返回true.

var thumbsEvent, isTouch = Modernizr.touch; // detect the touch
if(isTouch){
   thumbsEvent = 'click';//on touch surface, click
}
else {
   thumbsEvent = 'mouseover';//on non touch surface, mouseover
}
Run Code Online (Sandbox Code Playgroud)

有没有办法管理这个问题.

示例小提琴

Pat*_*ick 15

代表Modernizr - 我们真的很抱歉.

Modernizr.touch已经Modernizr.touchevents在尚未发布的版本3.0中重命名,因为它是一个更准确的检测描述.基本上,所有这些检测都在检查是否存在触摸事件,如果找到它们则返回true.如果启用开发人员工具,桌面chrome会执行相同的操作.这只是意味着您的笔记本电脑上的firefox版本报告了触摸事件的支持,原因可能有多种.


Uoo*_*ooo 1

我前段时间也遇到过同样的问题。

问题是某些笔记本电脑型号配备了触摸屏版本。我们发现,如果一个人使用这样的型号,Modernizr.touch即使此人使用的是非触摸笔记本电脑型号版本,也会返回 true。

现在,点击事件可以在触摸设备上运行,但反之则不行。因此,我们通过添加额外的检查来解决此限制:

var thumbsEvent, isTouch = Modernizr.touch;
var isAndroid = ...; // Android detection code
var isIOs = ...; // iOS detection code

// touch is only supported on iOS and Android
// devices, which have for sure a touch interface
if(isTouch && (isAndroid || isIOs) ){
    thumbsEvent = 'click';
}
else {
    thumbsEvent = 'mouseover';
}
Run Code Online (Sandbox Code Playgroud)

这可能不是最佳解决方案,因为您需要使用用户代理嗅探而不是功能检测来进行设备检测,这可能就是您首先使用 Modernizr 的原因。

此外,支持触摸但既不是 iOS 也不是 Android 的设备将被排除在触摸事件之外。