我目前在使用的插件中遇到一些触摸检测问题。
插件使用如下代码
touch = ("ontouchstart" in window) || window.DocumentTouch && document instanceof DocumentTouch;
eventType = (touch) ? "touchend" : "click";
确定是否应该在某些图库导航上使用 touchend 或 click 事件。
但是不幸的是,当使用运行 os 6.0 的黑莓 9300 访问该页面时,它被错误地报告为支持触摸的设备并且事件没有触发。
我检查了使用的检测方法,它与Modernizr中的相同。
有人能解决这个问题吗?
最佳答案
根据 Paul Irish, RIM said关于这个问题:
Unfortunately, we had a build system issue during BlackBerry 6.0 that caused builds to have WebKit touch support enabled, even for non-touch devices. It's long been fixed, but some public builds do have this issue.
在 github/Modernizr 上查看这些(当前打开的)票证,了解可能的解决方法和最新的检测代码,然后根据需要尝试更改您的插件。如果下面的最新检测代码不起作用,您可能需要专门检测黑莓。
同时检查 this touch test out,browserscope 选项卡指示 blackberry 9000 已被检测为假的,因此也值得一试在您的设备中对其进行测试。 http://modernizr.github.com/Modernizr/touch.html
latest modernizr除了您发布的代码之外,触摸检测的来源似乎还添加了@media 检测。
/*
* The Modernizr.touch test only indicates if the browser supports
* touch events, which does not necessarily reflect a touchscreen
* device, as evidenced by tablets running Windows 7 or, alas,
* the Palm Pre / WebOS (touch) phones.
*
* Additionally, Chrome (desktop) used to lie about its support on this,
* but that has since been rectified: crbug.com/36415
*
* We also test for Firefox 4 Multitouch Support.
*
* For more info, see: modernizr.github.com/Modernizr/touch.html
*/
tests['touch'] = function() {
var bool;
if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
bool = true;
} else {
injectElementWithStyles(['@media (',prefixes.join('touch-enabled),('),mod,')','{#modernizr{top:9px;position:absolute}}'].join(''), function( node ) {
bool = node.offsetTop === 9;
});
}
return bool;
};
BlackBerry/PlayBook UA 嗅探
使用用户代理字符串并借鉴 here 给出的解决方案专门检测黑莓设备和 here我创建了这个小函数,您可以在 http://jsbin.com/aliwur/1/edit#javascript,live 测试并查看在 jsbin 上的工作情况它应该从用户代理字符串中解析出 Blackberry 5.0/4.0/6.0 和 Playbook:
function rim_useragent_parser(ua) {
var info = false,
model = null,
model_number = null,
os_version = null;
if (ua.indexOf("BlackBerry") >= 0) {
if (ua.indexOf("Version/") >= 0) {
// BlackBerry 6 and 7
model = ua.match(/BlackBerry\s[0-9]*/);
if (model) {
model_number = model[0].match(/[0-9]+/);
pos = ua.indexOf("Version/") + 8;
os_version = ua.substring(pos, pos + 3);
info = {
'model' : model[0],
'model_number' : model_number[0],
'os_version' : os_version
};
}
}
else {
// BlackBerry Device Software 4.2 to 5.0
model = ua.match(/^BlackBerry[0-9]*/);
if (model) {
model_number = model[0].match(/[0-9]+/);
var SplitUA = ua.split("/");
os_version = SplitUA[1].substring(0, 3);
info = {
'model' : model[0],
'model_number' : model_number[0],
'os_version' : os_version
};
}
}
}
else if (ua.indexOf("PlayBook") >= 0) {
// PlayBook
model = ua.match(/RIM Tablet OS\s[0-9].[0-9].[0-9]/);
if (model) {
model_number = model[0].match(/[0-9].[0-9].[0-9]/);
pos = ua.indexOf("Version/") + 8;
os_version = ua.substring(pos, pos + 5);
info = {
'model' : model[0],
'model_number' : model_number[0],
'os_version' : os_version
};
}
}
return info;
}
当然,这可能比您需要的更多,因此将其简化为仅“Blackberry 9300 6.0”我想您也可以这样做:
var ua = navigator.userAgent;
if (ua.indexOf("BlackBerry") >= 0) {
if (ua.indexOf("Version/") >= 0) {
// BlackBerry 6 and 7
var model = ua.match(/BlackBerry\s[0-9]*/);
if (model) {
var model_number = model[0].match(/[0-9]+/);
if (model_number) model_number = model_number[0];
pos = ua.indexOf("Version/") + 8;
os_version = ua.substring(pos, pos + 3);
if (os_version === '6.0' && model_number === '9300') {
// do what you need specifically for this
}
}
}
}
为了更好的通用用户代理解析,请参阅 ua-parser
关于javascript - 触摸检测黑莓9300 6.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11827446/