Chrome 移动模拟模式中的 (maxTouchPoints) 和(文档中的“ontouchstart”)错误

2024-03-04

我使用这样的触摸屏设备检测:

if (window.navigator.maxTouchPoints || 'ontouchstart' in document)  
    // handle as mobile device
else
    // handle as desktop

当我在 Chrome 移动模拟中更改屏幕时,两者的结果maxTouchPoints and 'ontouchstart' in document是不可预测的。

对于同一个模拟屏幕,它可能会返回maxTouchPoints等于 0 或 1,并且'ontouchstart' in document等于true or false.

所以,我真的不能接受这张支票。
你能推荐一种方法来解决这个问题吗?


我创建了以下函数来检查触摸点是否实际启用(例如,使用设备/模拟器上的“启用触摸点按钮”):

function isTouchEventsEnabled() {
    // Bug in FireFox+Windows 10, navigator.maxTouchPoints is incorrect when script is running inside frame.
    // TBD: report to bugzilla.
    const navigator = (window.top || window).navigator;
    const maxTouchPoints = Number.isFinite(navigator.maxTouchPoints) ? navigator.maxTouchPoints : navigator.msMaxTouchPoints;
    if (Number.isFinite(maxTouchPoints)) {
        // Windows 10 system reports that it supports touch, even though it acutally doesn't (ignore msMaxTouchPoints === 256).
        return maxTouchPoints > 0 && maxTouchPoints !== 256;
    }
    return 'ontouchstart' in window;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Chrome 移动模拟模式中的 (maxTouchPoints) 和(文档中的“ontouchstart”)错误 的相关文章

随机推荐